zendframework / zend-inputfilter

InputFilter component from Zend Framework
BSD 3-Clause "New" or "Revised" License
64 stars 50 forks source link

ZF3 - type issue field for fieldsets or nested input filter #81

Open Wilt opened 8 years ago

Wilt commented 8 years ago

This issue describes a problem with the ZF2 input filtering that I would like to be fixed in the future ZF3 solution for input filtering.

Our preferred solution for building input filters in our application is by using config arrays and generating them through the input filter factory class. We often have nested input filters (for validating field-sets or nested resources). To reuse input filters inside other configs, when using field-sets or when handling complex nested resources it is necessary to add a type field to the config as follows:

$config = array(
    'resource' => array(
        'property_one' => array(
            'name' => 'property_one',
            'required' => false
        ),
        'property_two' => array(
            'name' => 'property_two',
            'required' => false
        ),
        'property_three' => array(
            'name' => 'property_three',
            'required' => false
        ),
        // type key necessary for nested input filter
        'type' => 'Zend\InputFilter\InputFilter'
    )
);

The factory will recognize the type key and sets it as the $class to use during building of the InputFilter. So now we can make our input filter like this:

$inputFilter = $factory->createInputFilter($config);

You can even another class as long as the class implements the InputFilterInterface

Now comes the issue... It is not uncommon to use the field type for data, in our case we have type as a property for several of our resources. This breaks the whole pattern of configuring our input filters with arrays. If we use the type field for input we can no longer set the input filter class in the config...

I think it is a design mistake to use a key with a common name like type at the same level as the input fields (properties) for setting the class in the config array. A solution could be to use a more specific key to make it less likely to have conflicts with input field names, for example input_class, or maybe even input_type. Probably it would even better to move the type field one level up in the config, so conflicts will never be an issue. The config could for example look like this:

$config = array(
    'resource' => array(
        'name' => 'resource',
        'class' => 'Zend\InputFilter\InputFilter', // declaring type
        'config' => array(
            'property_one' => array(
                'name' => 'property_one',
                'required' => false
            ),
            'property_two' => array(
                'name' => 'property_two',
                'required' => false
            ),
            'property_three' => array(
                'name' => 'property_three',
                'required' => false
            )
        )
    )
);

I have no idea about what the design for ZF3 input filtering looks like, but I just want to make sure that this issue would be solved in case it might be done similarly in ZF3.

macabot commented 8 years ago

I have the same problem. I hope this will be solved soon.

weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-inputfilter; a new issue has been opened at https://github.com/laminas/laminas-inputfilter/issues/8.