How do I configure a custom form element with zend expressive

139 views Asked by At

In my ZF2 Application, I had several custom form Elements with an injected database Adapter. I put the configuration in the module.php file with a method, like this:

            public function getFormElementConfig()
            {
                return array(
                    'factories' => [
                        'dksCodeElementSelect' => function($container)
                        {
                            $db = $container->get(AdapterInterface::class);
                            $elemnt = new Form\Element\Select\DksCodeElementSelect();
                            $elemnt->setDb($db);
                            return $elemnt;
                        },
                )
            }

How can I configure custom form elements within a zend-expressive application?

1

There are 1 answers

1
Mehmet SÖĞÜNMEZ On BEST ANSWER

Form class calls elements via 'FormElementManger'. and FormElementManager reads 'form_elements' key from config. It's basicly a container (service manager) so you have to configure it same as container (factories, invokables, aliases etc). Also, you have to register Zend/Form module to container. I didn't try it but has to be ths way;

(ps: it's too late here, if it doesn't work let me know so i can put a working example.)

class MyModule\ConfigProvider {
    public function __invoke()
    {
        return [
            'dependencies' => $this->getDependencies(),
            'templates'    => $this->getTemplates(),
            'form_elements => [
                 /* this is where you will put configuration */
                 'factories' => [
                    MyElement::class => MyElementFactory::class,
                 ]
            ]
       ];
   }
}