Skip to content

[Validator] Propagate embedded groups for collection validator #17696

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

Closed
wants to merge 3 commits into from
Closed

[Validator] Propagate embedded groups for collection validator #17696

wants to merge 3 commits into from

Conversation

blazarecki
Copy link

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #17675
License MIT
Doc PR no

This PR fix the issue describe in #17675

@@ -60,7 +60,7 @@ public function validate($value, Constraint $constraint)
$context->getValidator()
->inContext($context)
->atPath('['.$field.']')
->validate($value[$field], $fieldConstraint->constraints);
->validate($value[$field], $fieldConstraint->constraints, $context->getGroup());
} else {
// 2.4 API
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the scope of this PR but shouldn't this be removed in 2.7 ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removed ? I check on the 2.8 and 3.0 branch and the code is the same.

@blazarecki
Copy link
Author

The issue is fixed for the current API.
But it fail on the 2.4 API.

For the 2.4 API I've try to use the group from context and from the constraint and both don't work.

Any ideas ?

Here the phpunit errors: https://travis-ci.org/symfony/symfony/jobs/107124774#L2515-L2525

There were 2 failures:

1) Symfony\Component\Validator\Tests\LegacyValidatorTest::testValidateMultipleGroupsForCollectionConstraint
Failed asserting that actual size 4 matches expected size 2.

/Users/benjamin.lazarecki/Github/symfony/src/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php:1108

2) Symfony\Component\Validator\Tests\LegacyValidatorTest::testValidateMultipleGroupsForAllConstraint
Failed asserting that actual size 4 matches expected size 2.

/Users/benjamin.lazarecki/Github/symfony/src/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php:1132

@backbone87
Copy link
Contributor

There are also problems with GroupSequence.

    public function testValidateGroupSequenceForCollectionConstraint()
    {
        $entity = new Entity();
        $entity->firstName = array('baz' => 2);

        $sequence = new GroupSequence(array('bar', 'foo'));
        $this->metadata->setGroupSequence($sequence);

        $this->metadata->addPropertyConstraint('firstName', new Collection(
            array(
                'fields' => array(
                    'baz' => array(
                        new Range(array('min' => 3, 'minMessage' => 'Group foo', 'groups' => 'foo')),
                        new Range(array('min' => 5, 'minMessage' => 'Group bar', 'groups' => 'bar')),
                    ),
                ),
            )
        ));

        $violations = $this->validate($entity, null, 'Default');

        $this->assertCount(1, $violations);
        $this->assertSame('Group bar', $violations[0]->getMessage());
    }

This test should fail, but I did not actually run it.

@blazarecki
Copy link
Author

@backbone87 I add the test for group sequence.

@backbone87
Copy link
Contributor

@blazarecki what should c1311b8 fix?

@blazarecki
Copy link
Author

Hi @backbone87,

With this fix, now the current group is propagate to the embedded constraints.
Then the executeConstraintValidators method, filters only the constraint that match the given group.

@dunglas
Copy link
Member

dunglas commented Feb 9, 2016

👍

GroupSequence can be fixed in another PR.

ping @symfony/deciders

@blazarecki blazarecki changed the title [Validator] Propage embedded groups for collection validator [Validator] Propagate embedded groups for collection validator Feb 9, 2016
@dunglas
Copy link
Member

dunglas commented Feb 11, 2016

Status: reviewed

@fabpot
Copy link
Member

fabpot commented Feb 12, 2016

👍

@@ -44,12 +44,12 @@ public function validate($value, Constraint $constraint)
$validator = $context->getValidator()->inContext($context);

foreach ($value as $key => $element) {
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints, $constraint->groups);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be $context->getGroup() too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @xabbuh

I try to use the context but the test fail, it's the only way I find to make it work:

1) Symfony\Component\Validator\Tests\Validator\LegacyValidator2Dot5ApiTest::testValidateMultipleGroupsForAllConstraint
Failed asserting that actual size 1 matches expected size 2.

symfony/src/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php:1129

2) Symfony\Component\Validator\Tests\Validator\LegacyValidatorLegacyApiTest::testValidateMultipleGroupsForAllConstraint
Failed asserting that actual size 1 matches expected size 2.

symfony/src/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php:1129

3) Symfony\Component\Validator\Tests\Validator\RecursiveValidator2Dot5ApiTest::testValidateMultipleGroupsForAllConstraint
Failed asserting that actual size 1 matches expected size 2.

Do you have another idea ?

@blazarecki
Copy link
Author

@webmozart what do you think of this PR ?

@blazarecki
Copy link
Author

@javiereguiluz any new about this PR ? Is there something missing ? Thanks.

foreach ($constraints as $constraint) {
if (in_array($this->getGroup(), $constraint->groups)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird to me. Why is this necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's necessary because for embedded constraint with multiple validation groups, the validator is execute for each validation groups.

With this example:

$entity = new Entity();
$entity->firstName = array('baz' => 2);

$this->metadata->addPropertyConstraint('firstName', new Collection(
    array(
        'fields' => array(
            'baz' => array(
                new Range(array('min' => 3, 'groups' => array('foo', 'bar'))),
            ),
        ),
    )
));

$violations = $this->validate($entity, null, array('foo', 'bar'));

We have two violations but we want only one.

Is there another way to fix that ?

@webmozart
Copy link
Contributor

Thank you for working on this PR @blazarecki! I think that in general, this functionality is nice. This PR needs some work however to ensure that we don't introduce bugs with this functionality.

Status: Needs work

@blazarecki
Copy link
Author

Thanks for your feedback, I'll work on it.

@fabpot
Copy link
Member

fabpot commented Jul 1, 2016

@blazarecki What's the status of this PR? Can you answer @webmozart's question?

@blazarecki
Copy link
Author

@webmozart what kind of work is needed for this PR to ensure that we don't introduce bugs with this functionality?

@nicolas-grekas
Copy link
Member

@blazarecki rebase needed please. Can you please check again and see what's missing? Anyone else to help review?

@blazarecki
Copy link
Author

Gitub cannot find the repository and branch so I create a new PR #25888

@blazarecki blazarecki closed this Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants