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

Validation group not applied correctly when validating a CollectionInputFilter #3

Open
weierophinney opened this issue Dec 31, 2019 · 1 comment

Comments

@weierophinney
Copy link
Member

While debugging a problem when validating a complex form containing collections and validation groups to limit the fields that are being validated I notice the following code in CollectionInputFilter:

        foreach ($this->data as $key => $data) {
            // ....
            if (null !== $this->validationGroup) {
                $inputFilter->setValidationGroup($this->validationGroup[$key]);
            }

If the CollectionInputFilter has a validation group then the validation group is indexed using the same key as the data. A validation group is assigned like this:

$collection = new CollectionInputFilter(...);
$collection->setValidationGroup(array(
    'a', 'b', 'c'
));

Now, for the first element in the list only property a is validated, property b for the second, etc.

Am I missing something in the way I assign my validation group? Or should the line where the validation group is assigned read:

                $inputFilter->setValidationGroup($this->validationGroup);

I can work around this by calling $collection->getInputFilter()->setValidationGroup(...) instead, however this prevents me of performing a single setValidationGroup call at the root of my form with a single nested array for the entire form.


Originally posted by @erikorbons at zendframework/zend-inputfilter#113

@weierophinney
Copy link
Member Author

Yep really strange, but can be used like this:

$inputFilter = new InputFilter();
$inputFilter->add([
    'name' => 'foo',
    'required' => true,
]);
$inputFilter->add([
    'name' => 'bar',
    'required' => true,
]);

$collection = new CollectionInputFilter();
$collection->setInputFilter($inputFilter);
$collection->setValidationGroup([0 => ['bar'], 1 => ['foo']]);

$collection->setData([
    0 => ['bar' => 'value'], // valid because only 'bar' in validation group
    1 => ['foo' => 'value2'] // valid because only 'foo' in validation group
]);
$collection->isValid();

Okay I am not sure this is desired behavior nor use case for this, but well can't think otherwise why setValidationGroup() is called in every foreach loop. And also there is a test for this so can't be fixed?
also data being array of 3 elements will throw Undefined offset: 2

@weierophinney what do you think? If this is a bug I can create PR with a fix.


Originally posted by @svycka at zendframework/zend-inputfilter#113 (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

1 participant