-
Notifications
You must be signed in to change notification settings - Fork 50
Input Filter Factory uses a different FilterPluginManager instance #79
Comments
This is expected behavior. To get the input filter as configured at the application level, you need to do the following:
Alternately, you can set all this up semi-manually. In your controller, you will do something like the following: $services = $this->getServiceLocator();
$inputFilters = $services->get('InputFilterManager');
$inputFilter = new YourCustomInputFilter();
$inputFilterFactory = $inputFilter->getFactory()->setInputFilterManager($inputFilters);
$inputFilter->init(); A simpler approach would be to register your input filter with the InputFilterManager. Assuming you have no constructor dependencies, it could be registered as an invokable: return [
'input_filters' => [
'invokables' => [
'YourCustomInputFilter' => 'FullyQualfied\Namespace\For\YourCustomInputFilter',
],
],
]; From there, you could do the following in your controller: $services = $this->getServiceLocator();
$inputFilters = $services->get('InputFilterManager');
$inputFilter = $inputFilters->get('YourCustomInputFilter'); Even better would be to inject it directly into your controller via the constructor, and to create a factory for your controller. The tutorial demonstrates such factories. What the above approaches do is ensure that any input filters or inputs you've defined in the InputFilterManager are injected in your custom input filter's internal factory — and this simultaneously pulls the configured FilterManager and ValidatorManager and sets them in the default filter and validator chains, ensuring that you have access to those filters and validators you defined in modules and/or the application level. The reason it works this way is because ZF2 does not define any globally static plugin managers; you must inject them where you want them. This prevents a whole category of problems we encountered in ZF1, and makes your code ultimately more testable (particularly if you inject the input filter into the controller instead of pulling it from the service manager within your controller). |
Thank you very much for this detailed answer! I want to define the input filters next to their related form elements inside a class extending When the object bound to the form does not implement $this->filter = new InputFilter(); In this case, the The form has a pointer to a Adding following line right after the statement shown above makes custom filters and validators available inside the form as expected: $this->filter->setFactory($this->getFormFactory()->getInputFilterFactory()); I wanted to continue the discussion here before opening a pull request in the zend-form repository. Let me know your thoughts about this! |
I created a custom filter which needs dependency injection by a factory. I added the filter to the config array like so:
I am able to get a filter instance from the
FilterPluginManager
inside a controller:Inside the
init()
method of a class extendingZend\Form\Form
I configured an input filter using the custom filter. The form also uses custom form elements from theFormElementManager
.Custom form elements are working fine but the custom filter always raises a
ServiceNotFoundException
:I found out that the Input Filter Factory is using a different
FilterPluginManager
instance which has never seen the config array. The custom filter with its factory is not included in the plugin manager.This is unexpected behaviour? Or am I wrong?
The text was updated successfully, but these errors were encountered: