-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Propagate embedded groups for collection validator #25888
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
Conversation
/propage/propagate/ in your commit message :) |
@@ -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.
To give more context: This implementation suffers the same implications as discussed in #23480 (comment).
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.
The Composite
constraint is special, All
and Collection
inherit it.
The rules are explained here https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/Composite.php#L34-L48, we need to not break them.
So I tend to agree with @xabbuh that we should not just rely on the groups
public property of nested constraints to resolve any issue.
@@ -60,10 +60,10 @@ public function validate($value, Constraint $constraint) | |||
$context->getValidator() | |||
->inContext($context) | |||
->atPath('['.$field.']') | |||
->validate($value[$field], $fieldConstraint->constraints); | |||
->validate($value[$field], $fieldConstraint->constraints, $fieldConstraint->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.
see above
can we please close either #23480 or this one? both address the same problem and propose the same fixing strategy. Using $contraint = new Collection([
'fields' => [
'key' => new Length(['min' => 6, groups => ['b']]),
],
'groups' => ['a', 'b'],
]);
$validator->validate([ 'key' => 'value' ], $contraint, 'a'); From my point of view we have a 2 possibilities:
|
I'm not sure there is really an issue here, just a misusage while not being aware of https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/Composite.php#L34-L48 (documentation issue). Actually #17675 (comment), could be written: /**
* @Collection(
* fields={
* "foo" = {
* @NotBlank(groups={"foo"}),
* @Length(min="2", groups={"bar"}),
* @Length(min="4", groups={"baz"}),
* }
* },
* groups={"foo", "bar", "baz"}
* )
*/ And this should be the way to go IMHO. |
This doesnt work with grp sequences |
So this is what needs to be fixed :). Could you please update your tests here @blazarecki? |
Yes I'll do that. |
@blazarecki imho there is nothing wrong with grp sequences. the possible fix path are described in my comment above. Despite the comment in https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/Composite.php#L34 different grps for composite contraint and nested constraints just dont work. If we dont want to change anything, then my proposed solution 1 is a good way. |
Any help needed with this one? Comments seem to be addressed and tests passing. |
@artursvonda tell me if I need to change something. |
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.
As said in my previous comment, there is no issue to fix as is.
Tests must be adapted to check if group sequences fail in some way as @backbone87 claimed.
Otherwise we can close here.
@@ -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, array($context->getGroup())); |
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.
Only subsets are allowed on traversable node. This would be a broken feature not a bug fix.
new Range(array('min' => 1, 'groups' => 'bar')), | ||
), | ||
), | ||
'groups' => 'foo', |
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 test is wrong and breaks BC. Groups should be array('foo, 'bar')
here, and the test would be green already, without the changes introduced in this PR.
array(array('foo'), array('foo', 'bar'), array('foo', 'bar')), | ||
array(array('foo'), array('bar'), array('foo', 'bar')), | ||
array(array('foo'), array('foo'), array('foo', 'bar')), | ||
array(array('foo'), array('foo'), array('foo')), |
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.
You're passing foo
and bar
for every case of collection groups above, but not here, while there is no bar group on any entry.
Are there some cases actually failing before your changes?
), | ||
), | ||
) | ||
)); |
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.
If there is no group set on the collection contraint, group on entry should never be validated (since it won't ever be a subset).
new Range(array('min' => 5, 'minMessage' => 'Group bar', 'groups' => 'bar')), | ||
), | ||
) | ||
)); |
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.
Same here.
Thank you all for your input and working on this. This is finally going to be fixed in #29499. |
This PR was merged into the 3.4 branch. Discussion ---------- [Validator] Fixed grouped composite constraints | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17675, #25888, #23480 | License | MIT | Doc PR | ~ From Lisbon :). Thanks @stof, @xabbuh for your help to finally fix this old issue. Commits ------- b53d911 [Validator] Fixed grouped composite constraints
This PR fix the issue describe in #17675
This PR is related to #17696