Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Custom element factory not called #174

@fezfez

Description

@fezfez

Hello,

I've followed this guide https://zendframework.github.io/zend-form/advanced/#handling-dependencies but i cant get it work.

module.config.php

<?php
...
    'form_elements' => array(
        'factories' => array(
            \Base\File\FileElement::class => \Base\File\FileElementFactory::class
        )
    ),
...

FileElementFactory

<?php

namespace Base\File;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class FileElementFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $config = $container->get('Config');

        $dir = $config['upload']; // Contain "myDir"

        if (!is_dir($dir)) {
            mkdir($dir);
        }

        var_dump('im call');

        return new FileElement($dir);
    }
}

FileElement

<?php
namespace Base\File;

use Zend\InputFilter\InputProviderInterface;

class FileElement extends \Zend\Form\Element\File implements InputProviderInterface
{
    private $dir;

    public function __construct($dir)
    {
        $this->dir = $dir;
    }

    public function getInputSpecification()
    {
        $validators = [
            ['name'    => 'FileUploadFile'],
        ];

        return array(
            'name'       => $this->getName(),
            'required'   => false,
            'validators' => $validators,
            'filters'  => [
                [
                    'name' => 'FileRenameUpload',
                    'options' => [
                        'target'=> $this->dir,
                        'useUploadName'=>false,
                        'useUploadExtension'=>false,
                        'overwrite'=>false,
                        'randomize'=>true
                    ]
                ]
            ],
            'options' => array(
                'label' => 'Fichier',
            ),
            'attributes' => array(
                'id' => 'file',
                'class'=> 'form-control'
            ),
        );
    }
}

Form

<?php
namespace StatementsSuppliers\Form;

use Zend\Form\Form;
use Base\File\FileElement;
use Zend\Stdlib\InitializableInterface;

class StatementsSuppliersForm extends Form implements InitializableInterface
{
    public function init()
    {
        $this->add([
            'type' => FileElement::class,
            'name' => 'filename',
            'options' => array(
                'label' => 'File',
            ),
            'attributes' => array(
                'id' => 'file',
                'class'=> 'form-control'
            )
        ]);
    }
}

Controller

<?php
namespace StatementsSuppliers\Controller;

use StatementsSuppliers\Form\StatementsSuppliersForm;
use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
{
    /**
     * @var StatementsSuppliersForm
     */
    private $statementsSuppliersForm;

    public function __construct(StatementsSuppliersForm $statementsSuppliersForm)
    {
        $this->statementsSuppliersForm = $statementsSuppliersForm;
    }

    public function addAction()
    {
        $this->statementsSuppliersForm->init();

        var_dump($this->statementsSuppliersForm->getElements());exit;
    }
}

Actual output

array(1) {
  ["filename"]=>
  object(Base\File\FileElement)#854 (8) {
    ["dir":"Base\File\FileElement":private]=>
    string(11) "fileelement"
    ["attributes":protected]=>
    array(4) {
      ["type"]=>
      string(4) "file"
      ["name"]=>
      string(8) "filename"
      ["id"]=>
      string(4) "file"
      ["class"]=>
      string(12) "form-control"
    }
    ["label":protected]=>
    string(7) "Fichier"
    ["labelAttributes":protected]=>
    array(0) {
    }
    ["labelOptions":protected]=>
    array(0) {
    }
    ["messages":protected]=>
    array(0) {
    }
    ["options":protected]=>
    array(1) {
      ["label"]=>
      string(7) "Fichier"
    }
    ["value":protected]=>
    NULL
  }
}

Expected output

string(7) "im call"
array(1) {
  ["filename"]=>
  object(Base\File\FileElement)#854 (8) {
    ["dir":"Base\File\FileElement":private]=>
    string(11) "myDir"
    ["attributes":protected]=>
    array(4) {
      ["type"]=>
      string(4) "file"
      ["name"]=>
      string(8) "filename"
      ["id"]=>
      string(4) "file"
      ["class"]=>
      string(12) "form-control"
    }
    ["label":protected]=>
    string(7) "Fichier"
    ["labelAttributes":protected]=>
    array(0) {
    }
    ["labelOptions":protected]=>
    array(0) {
    }
    ["messages":protected]=>
    array(0) {
    }
    ["options":protected]=>
    array(1) {
      ["label"]=>
      string(7) "Fichier"
    }
    ["value":protected]=>
    NULL
  }
}

composer show

zendframework/zend-authentication            2.5.3             provides an API for authentication and includes c...
zendframework/zend-cache                     2.7.2             provides a generic way to cache any data
zendframework/zend-code                      2.6.3             provides facilities to generate arbitrary code us...
zendframework/zend-component-installer       1.1.x-dev 960fc8a Composer plugin for automating component registra...
zendframework/zend-config                    3.1.0             provides a nested object property based user inte...
zendframework/zend-console                   2.6.0
zendframework/zend-debug                     2.5.1
zendframework/zend-developer-tools           1.1.1             Module for developer and debug tools for use with...
zendframework/zend-di                        2.6.1
zendframework/zend-dom                       2.6.0             provides tools for working with DOM documents and...
zendframework/zend-escaper                   2.5.2
zendframework/zend-eventmanager              3.2.0             Trigger and listen to events within a PHP applica...
zendframework/zend-file                      2.7.1
zendframework/zend-filter                    2.7.2             provides a set of commonly needed data filters
zendframework/zend-form                      2.10.2
zendframework/zend-http                      2.6.0             provides an easy interface for performing Hyper-T...
zendframework/zend-hydrator                  2.2.2
zendframework/zend-i18n                      2.7.4
zendframework/zend-inputfilter               2.7.4
zendframework/zend-json                      3.0.0             provides convenience methods for serializing nati...
zendframework/zend-loader                    2.5.1
zendframework/zend-log                       2.9.2             component for general purpose logging
zendframework/zend-mail                      2.8.0             provides generalized functionality to compose and...
zendframework/zend-mime                      2.6.1
zendframework/zend-modulemanager             2.8.0
zendframework/zend-mvc                       3.1.0
zendframework/zend-mvc-i18n                  1.0.0
zendframework/zend-mvc-plugin-flashmessenger 1.0.0
zendframework/zend-mvc-plugin-identity       1.0.0
zendframework/zend-paginator                 2.7.0
zendframework/zend-permissions-acl           2.6.0             provides a lightweight and flexible access contro...
zendframework/zend-router                    3.0.2
zendframework/zend-servicemanager            3.3.0
zendframework/zend-session                   2.8.0             manage and preserve session data, a logical compl...
zendframework/zend-stdlib                    3.1.0
zendframework/zend-test                      3.1.0
zendframework/zend-uri                       2.5.2             a component that aids in manipulating and validat...
zendframework/zend-validator                 2.9.2             provides a set of commonly needed validators
zendframework/zend-view                      2.9.0             provides a system of helpers, output filters, and...
zfcampus/zf-development-mode                 3.1.0             Zend Framework development mode script

Thanks in advance !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions