-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
[Validator] Propagate embedded groups for collection validator #17696
Conversation
@@ -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 |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
The issue is fixed for the current 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 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. |
@backbone87 I add the test for group sequence. |
@blazarecki what should c1311b8 fix? |
Hi @backbone87, With this fix, now the current group is propagate to the embedded constraints. |
👍
ping @symfony/deciders |
Status: reviewed |
👍 |
@@ -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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ?
@webmozart what do you think of this PR ? |
@javiereguiluz any new about this PR ? Is there something missing ? Thanks. |
foreach ($constraints as $constraint) { | ||
if (in_array($this->getGroup(), $constraint->groups)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ?
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 |
Thanks for your feedback, I'll work on it. |
@blazarecki What's the status of this PR? Can you answer @webmozart's question? |
@webmozart what kind of work is needed for this PR to ensure that we don't introduce bugs with this functionality? |
@blazarecki rebase needed please. Can you please check again and see what's missing? Anyone else to help review? |
Gitub cannot find the repository and branch so I create a new PR #25888 |
This PR fix the issue describe in #17675