Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom element factory not called #12

Closed
weierophinney opened this issue Dec 31, 2019 · 4 comments
Closed

Custom element factory not called #12

weierophinney opened this issue Dec 31, 2019 · 4 comments

Comments

@weierophinney
Copy link
Member

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 !


Originally posted by @fezfez at zendframework/zend-form#174

@weierophinney
Copy link
Member Author

I've found the bug in my code

to get my form instance in my controller factory was doing

$container->get('myForm')

instead of

$container->get('FormElementManager')->get('myForm')

and i add my form factory to

    'form_elements' => array(
        'factories' => array(
            'myForm' => 'myFormFactory'
        )
    ),

Maybe documentation need to be fix, this part is only on "Creating custom elements", but i follow only "Handling dependencies", i was thinking that's it was complete sample...


Originally posted by @fezfez at zendframework/zend-form#174 (comment)

@weierophinney
Copy link
Member Author

I want to access the Factory to edit the render method of custom Element.
And how to solve the problem, if you use to call the form AnnotationBuilder?

ExampleEntity

<?php
...
use Zend\Form\Annotation;
...
/**
 * @AnnotationForm\Type("Application\Form\Element\CustomElement")
 */
public $custom;
...

IndexController

<?php
...
$class = $this->params()->fromQuery('entity')
$b = new AnnotationBuilder();
$form = $b->createForm($class)
...

module.config.php

<?php
...
'form_elements' => [
        'factories' => [
            \Application\Form\Element\CustomElement::class => \Application\Form\Element\CustomFactory::class
        ]
],

Debug $form->getFormFactory()

<?php
...
["Application\Form\Element\CustomElement"] => object(Zend\Form\ElementFactory)#454 (1) {
        ["creationOptions":"Zend\Form\ElementFactory":private] => NULL
      }
...

Originally posted by @korolev-kg at zendframework/zend-form#174 (comment)

@weierophinney
Copy link
Member Author

@korolev-kg
Your problem has nothing to do with this issue report.
Please use the chat or the forum for questions or create a new issue if you found a bug. Thanks!


Originally posted by @froschdesign at zendframework/zend-form#174 (comment)

@samsonasik
Copy link
Member

closing as nothing todo with issue report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants