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.
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.
module.config.php
by input_filters top key, not filter, filters, filter_manger, filtermanager etc..Zend\InputFilter\InputFilter
when writing your own GlassFilter
.init()
method of GlassFilter
, not in the __constructor()
. It will be called automatically after construction.inputfiltermanager
, not servicemanager
directly.Config example:
'input_filters' => array(
'invokables' => array(
'glassfilter' => '\Application\Filter\GlassFilter',
),
),
Usage example:
$glassfilter = $serviceLocator->get('InputFilterManager')->get('glassfilter');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With