Documentation

Input filter specifications — Zend Framework 2 2.4.9 documentation

Zend\InputFilter

Input filter specifications

Zend\InputFilter allows configuration-driven creation of input filters via Zend\InputFilter\InputFilterAbstractServiceFactory. This abstract factory is responsible for creating and returning an appropriate input filter given named configuration under the top-level configuration key input_filter_specs.

It is registered with Zend\InputFilter\InputFilterPluginManager, allowing you to pull the input filter via that plugin manager. A side effect is that forms pulled from Zend\Form\FormElementManager can use these named input filters.

Setup

This functionality is disabled by default.

To enable it, you must add the Zend\InputFilter\InputFilterAbstractServiceFactory abstract factory to the Zend\InputFilter\InputFilterPluginManager configuration, which is unser the input_filters configuration key.

1
2
3
4
5
6
7
return array(
    'input_filters' => array(
        'abstract_factories' => array(
            'Zend\InputFilter\InputFilterAbstractServiceFactory'
        ),
    ),
);

Example

In the following code, we define configuration for an input filter named foobar:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
return array(
    'input_filter_specs' => array(
        'foobar' => array(
            0 => array(
                'name' => 'name',
                'required' => true,
                'filters' => array(
                    0 => array(
                        'name' => 'Zend\Filter\StringTrim',
                        'options' => array(),
                    ),
                ),
                'validators' => array(),
                'description' => 'Hello to name',
                'allow_empty' => false,
                'continue_if_empty' => false,
        ),
    ),
);

When creating a controller, we might then pull the InputFilterManager, and retrieve the foobar input filter we’ve defined in order to inject it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MyValidatingControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $controllers)
    {
        // Retrieve the application service manager
        $services = $controllers->getServiceLocator();

        // Retrieve the InputFilterManager
        $filters = $services->get('InputFilterManager');

        // Instantiate the controller and pass it the foobar input filter
        return new MyValidatingController($filters->get('foobar'));
    }
}

And you can use it, as you already did with other input filters:

1
2
3
4
5
6
7
$inputFilter->setData(array(
    'name' => 'test',
));

if (! $inputFilter->isValid()) {
    echo 'Data invalid';
}

Copyright

© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts