You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello everyone. I have the problem with yaml validation of collection with fields of different groups. I debugged a lot, and I came to the conclusion that there is a bug in validation of \Symfony\Component\Validator\Constraints\Collection with different groups.
Example description
For example, we had two equal entities: User and Category. Both have id, modifier and info (the array of some additional information with keys id, title and text).
– Root field id is required (I mean it should have the NotBlank constraint).
– Second field modifier is required only on update, so we add custom validation group called modify. We assume, on create we won't pass this group, but we will pass it on update.
– Third field info it is a large collection of different non-required parameters: in our case they are id (some other identification), title (length should be between 3 and 25 chars, only with group info) and text (length should be between 3 and 25 chars). I added additional validation group info for title because I wanted to check it not always, only when group is passed.
Problem reproduce
I prepared a little repository for tests to figure out the problem. The original successful code is located in validation branch of Aliance/SymfonyTest repository: https://github.com/Aliance/SymfonyTest/tree/validation
The failed code is located into validation-collection-with-different-groups branch, see pr: Aliance/SymfonyTest#1
If you execute phpunit tests you will find out that they fails after adding second group in other field on collection.
Note that tests fails only with yaml validation!
What I have already debugged
– First of all, code execution runs to \Symfony\Component\Validator\Validator\RecursiveContextualValidator::validate with my object as a $value and all three passed groups ['Default','modify','info'] as a $groups
– Later it iterates on each groups on every field. Reaching the info type it will raise \Symfony\Component\Validator\Constraints\CollectionValidator::validate. It will repeat validation on every field without any groups passed:
Looks strange, seems to me that CollectionValidator should pass $fieldConstraint->groups to validate method...
– But later somehow valid groups are came to validator: \Symfony\Component\Validator\Validator\RecursiveContextualValidator::validateEachObjectIn:
– But in deeps of this method, checked constraint-group has already cached as validated, because cache key only consist of spl_object_hash($constraint), without validation group handling: see https://github.com/symfony/symfony/blob/v2.8.22/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php#L866-L872
Can anyone approve this bug or tell me what I am doing wrong?
The text was updated successfully, but these errors were encountered:
Seems like #11505 broke groups handling at Symfony 2.5.
Aliance
changed the title
[Validation] Incorrect collection validation with different groups
[Validator] Incorrect collection validation with different groups
Jul 13, 2017
Hello everyone. I have the problem with yaml validation of collection with fields of different groups. I debugged a lot, and I came to the conclusion that there is a bug in validation of
\Symfony\Component\Validator\Constraints\Collection
with different groups.Example description
For example, we had two equal entities:
User
andCategory
. Both haveid
,modifier
andinfo
(the array of some additional information with keysid
,title
andtext
).– Root field
id
is required (I mean it should have theNotBlank
constraint).– Second field
modifier
is required only on update, so we add custom validation group calledmodify
. We assume, on create we won't pass this group, but we will pass it on update.– Third field
info
it is a large collection of different non-required parameters: in our case they areid
(some other identification),title
(length should be between 3 and 25 chars, only with groupinfo
) andtext
(length should be between 3 and 25 chars). I added additional validation groupinfo
fortitle
because I wanted to check it not always, only when group is passed.Problem reproduce
I prepared a little repository for tests to figure out the problem. The original successful code is located in
validation
branch ofAliance/SymfonyTest
repository: https://github.com/Aliance/SymfonyTest/tree/validationThe failed code is located into
validation-collection-with-different-groups
branch, see pr: Aliance/SymfonyTest#1If you execute phpunit tests you will find out that they fails after adding second group in other field on collection.
Note that tests fails only with yaml validation!
What I have already debugged
– First of all, code execution runs to



\Symfony\Component\Validator\Validator\RecursiveContextualValidator::validate
with my object as a$value
and all three passed groups['Default','modify','info']
as a$groups
– Later it iterates on each groups on every field. Reaching the
info
type it will raise\Symfony\Component\Validator\Constraints\CollectionValidator::validate
. It will repeat validation on every field without any groups passed:Looks strange, seems to me that
CollectionValidator
should pass$fieldConstraint->groups
tovalidate
method...– But later somehow valid groups are came to validator:
\Symfony\Component\Validator\Validator\RecursiveContextualValidator::validateEachObjectIn
:– But in deeps of this method, checked constraint-group has already cached as validated, because cache key only consist of
spl_object_hash($constraint)
, without validation group handling: see https://github.com/symfony/symfony/blob/v2.8.22/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php#L866-L872Can anyone approve this bug or tell me what I am doing wrong?
The text was updated successfully, but these errors were encountered: