C
C
Cat Anton2015-03-19 15:23:10
Zend Framework
Cat Anton, 2015-03-19 15:23:10

How to force Zend\Form\Factory to create usable filters via FilterPluginManage when creating forms?

In the configuration file of the module, I registered my own filter to create it through the FilterPluginManage:

return [
    // ...
    'filters' => [
        'invokables' => [
            'myfilter' => Application\Filter\MyFilter::class,
        ]
    ]
];

I can't figure out how to tell Zend\Form\Factory about my filter, I keep getting "Zend\Filter\FilterPluginManager::get was unable to fetch or create an instance for myfilter" exceptions. It is also important for me that my filter is created via FilterPluginManage.
For example, a similar task, but not with filters, but with custom elements specified in the 'form_elements' section of the configuration, is solved like this:
$formElementManager = $this->getServiceLocator()->get('FormElementManager');
$factory = new Zend\Form\Factory($formElementManager);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cat Anton, 2015-03-20
@27cm

This option works:

$filterManager = $this->getServiceLocator()->get('FilterManager');
$factory = new Zend\Form\Factory();
$factory->getInputFilterFactory()
        ->getDefaultFilterChain()
        ->setPluginManager($filterManager);

Also, judging by the discussion here, filters should be picked up if they are listed in the Module::getFilterConfig() method:
class Module implements Zend\ModuleManager\Feature\FilterProviderInterface
{
    // ...

    /**
     * {@inheritDoc}
     */
    public function getFilterConfig()
    {
        return [
            'invokables' => [
                'myfilter' => \Application\Filter\MyFilter::class,
            ],
        ];
    }
}

But it does not work, and indeed it would be strange if it worked. I always thought that there is no difference in where to declare: in module methods or module.config.php

A
Andrey Mokhov, 2015-03-19
@mokhovcom

If I were you, in debug mode, I would see if the FilterPluginManager picked up the config settings, maybe something is wrong with the config?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question