Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
weierophinney opened this issue Dec 31, 2019 · 2 comments
Open

Comments

@weierophinney
Copy link
Member

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.


Originally posted by @Wilt at zendframework/zend-inputfilter#81

@weierophinney
Copy link
Member Author

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


Originally posted by @macabot at zendframework/zend-inputfilter#81 (comment)

@alex-patterson-webdev
Copy link

Same. I've created #24 if someone would like to take a look or comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants