Issue 1:
Q
A

Bug report?
yes

Feature request?
no

BC Break report?
no

RFC?
no

Symfony version
2.7.*

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?

Issue 2:
Hi,

I don't know if it's a bug but it seem that all embedded constraints of a collection constraint are not applied when using validation groups.

I think that only the first validation group passed to the validator is used and the other are omitted.

With the following example if $config is equal to ['foo' => 'a'] then with validation groups:

['foo', 'bar', 'baz'] we have 0 violation (NotBlank)

['baz', 'bar', 'foo'] we have 1 violation (Lenght(min="4"))

['bar', 'baz', 'foo'] we have 1 violation (Lenght(min="2"))

/**
* @Collection(
* fields={
* "foo" = {
* @NotBlank(groups={"foo"}),
* @Length(min="2", groups={"bar"}),
* @Length(min="4", groups={"baz"})
* }
* }
* )
*/
public $config;

Here the complete gist with the tests : https://gist.github.com/blazarecki/5dcb754be22dae4f0e53

Any idea ?

Thanks.