cannot find service filter plugin Zend Framework

187 Views Asked by At

Ive got the following configuration for my filters. this is used for zend framework's setup of service manager.

$filters = [
  'factories' => ['Administration\Filter\StripSpaces'=>'Zend\ServiceManager\Factory\InvokableFactory']
  'aliases'   => ['StripSpaces'=>'Administration\Filter\StripSpaces']
];
return ['filters'=>$filters];

For the form usage

$inputFilter->add([
    'name'     => 'objectclassname',
    'required' => true,
    'filters'  => [
            ['name' => 'StringTrim'],
            ['name' => 'StripTags'],
            ['name' => 'StripNewlines'],
            ['name' => 'StripSpaces'] // here is where my StripSpaces alias is used
    ],
    'validators' => [                       
            [
             'name'    => 'StringLength',
             'options' => [
                'min' => 5,
                'max' => 255
              ],
         ]
     ],
]);

And here's the error when loading the form that uses this filter:

A plugin by the name "StripSpaces" was not found in the plugin manager Zend\Filter\FilterPluginManager
2

There are 2 best solutions below

1
FranMercaes On

As far as I know, there is no filter called StripSpaces in ZF3.

0
Kwido On

If you want to have custom filters/validators within your InputFilter you should retrieve the InputFilter class from your InputFilterManager as in:

$serviceManager->get('InputFilterManager')->get(MyInputFilter::class);

Or use any alias instead of the FQCN, it's based on how you registered your inputFilters.

Why do I need to fetch my inputfilter from the manager? Due to the fact that when you create a new object, as in new MyInputFilter(), the InputFilter\Factory creates a new instance of the InputFilterManager class. This is not the applications InputFilterManager which contains your configuration, thus only the default Zend filters/validators. This is fine to use if you don't have any custom filters or validators you want to use.

When you use the applications InputFilterManager to fetch your inputfilter, it updates the factory by providing the applications InputFilterManager which contains a reference to the applications ServiceManager. And from your application ServiceManager it fetches the other managers for your filters and validators and updates the chains of the InputFilter Factory. So the InputFilter is aware of your custom filters/validators.

Note that you should update the way you setup your input filters. Do not setup your filters/validators in the __construct() as the chains are not yet updated and thus do not contain your custom filters/validators. Move your input filter configuration to the public function init() which is called from the InputFilterManager, which initializes your InputFilter class.