propagate groups to nested constraints

This commit is contained in:
Christian Flothmann 2020-12-28 15:57:13 +01:00
parent 29b41edf06
commit 13a4610824
2 changed files with 25 additions and 1 deletions

View File

@ -34,7 +34,7 @@ class AtLeastOneOfValidator extends ConstraintValidator
$messages = [$constraint->message];
foreach ($constraint->constraints as $key => $item) {
$violations = $validator->validate($value, $item);
$violations = $validator->validate($value, $item, $this->context->getGroup());
if (0 === \count($violations)) {
return;

View File

@ -28,6 +28,7 @@ use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\Unique;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Validation;
/**
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
@ -160,4 +161,27 @@ class AtLeastOneOfValidatorTest extends ConstraintValidatorTestCase
]],
];
}
public function testGroupsArePropagatedToNestedConstraints()
{
$validator = Validation::createValidator();
$violations = $validator->validate(50, new AtLeastOneOf([
'constraints' => [
new Range([
'groups' => 'non_default_group',
'min' => 10,
'max' => 20,
]),
new Range([
'groups' => 'non_default_group',
'min' => 30,
'max' => 40,
]),
],
'groups' => 'non_default_group',
]), 'non_default_group');
$this->assertCount(1, $violations);
}
}