How to use InputFilterManager to construct custom InputFilters in Zf2

↘锁芯ラ 提交于 2019-12-03 02:59:58
edigu

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');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!