9

ZF2 documentation says following on defult services documentation;

InputFilterManager, mapping to Zend\Mvc\Service\InputFilterManagerFactory. This creates and returns an instance of Zend\InputFilter\InputFilterPluginManager, which can be used to manage and persist input filter instances.

I have a custom zf2 inputfilter class and i'm adding filters and validators inside init() method like following;

namespace Application\Filter;
use Zend\InputFilter\InputFilter;

class GlassFilter extends InputFilter
{
    public function init()
    {
        $this->add(array(
                'name' => 'glassname',
                'required' => true,
                'filters' => array(
                    array('name' => 'StringToUpper'),
                ),
                'validators' => array(
                    array( 'name' => 'StringLength', 'options' => array('min' => 3),
                ),
        ));
}

Also i added following key to my module.config.php

'filters' => array(
    'invokables' => array(
        'glassfilter' => '\Application\Filter\GlassFilter',
    ),
),

My question is, how can i construct my GlassFilter using InputFilterManager? Is this a correct approach? I found this thread but i want to understand relation between custom InputFilters and InputFilterManager.

1
  • If im not mistaken, as long as you register it, it should be as simple as array('name' => 'glassfilter') under the 'filters'-key Commented May 17, 2013 at 11:05

1 Answer 1

30

Ok, after spending 3 bloody hours (thanks to incredible(!) documentation) I figured it out. I'm writing my solution as an answer, hopefully it will help others who want to write their custom inputfilters.

  1. You should register your custom inputfilter in module.config.php by input_filters top key, not filter, filters, filter_manger, filtermanager etc..
  2. Extend default Zend\InputFilter\InputFilter when writing your own GlassFilter.
  3. Write your filters inside the init() method of GlassFilter, not in the __constructor(). It will be called automatically after construction.
  4. Then get it anywhere via inputfiltermanager, not servicemanager directly.

Config example:

'input_filters' => array(
    'invokables' => array(
        'glassfilter' => '\Application\Filter\GlassFilter',
     ),
),

Usage example:

$glassfilter = $serviceLocator->get('InputFilterManager')->get('glassfilter');
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Thank you for number 4! As for 1 you can also alternatively use the Zend\ModuleManager\Feature\FilterProviderInterface / getFilterConfig() in Module.php.
Thanks for detail explanation. In my case the init() function was not being triggered. This made it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.