From e5530939b33cd672e21a3eaf1357597c10edf7ef Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 6 Aug 2022 13:08:17 +0200 Subject: [PATCH] validate nested constraints only if they are in the same group --- .../Constraints/AtLeastOneOfValidator.php | 4 ++++ .../Constraints/AtLeastOneOfValidatorTest.php | 23 ++++++++++++++++++ .../Constraints/SequentiallyValidatorTest.php | 24 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/Symfony/Component/Validator/Constraints/AtLeastOneOfValidator.php b/src/Symfony/Component/Validator/Constraints/AtLeastOneOfValidator.php index 95558519d8510..888f583eb92b6 100644 --- a/src/Symfony/Component/Validator/Constraints/AtLeastOneOfValidator.php +++ b/src/Symfony/Component/Validator/Constraints/AtLeastOneOfValidator.php @@ -34,6 +34,10 @@ public function validate($value, Constraint $constraint) $messages = [$constraint->message]; foreach ($constraint->constraints as $key => $item) { + if (!\in_array($this->context->getGroup(), $item->groups, true)) { + continue; + } + $executionContext = clone $this->context; $executionContext->setNode($value, $this->context->getObject(), $this->context->getMetadata(), $this->context->getPropertyPath()); $violations = $validator->inContext($executionContext)->validate($value, $item, $this->context->getGroup())->getViolations(); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php index 6be6a5d6f702c..0fb735a84cdb2 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php @@ -19,6 +19,7 @@ use Symfony\Component\Validator\Constraints\DivisibleBy; use Symfony\Component\Validator\Constraints\EqualTo; use Symfony\Component\Validator\Constraints\Expression; +use Symfony\Component\Validator\Constraints\GreaterThan; use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; use Symfony\Component\Validator\Constraints\IdenticalTo; use Symfony\Component\Validator\Constraints\Language; @@ -235,6 +236,28 @@ public function hasMetadataFor($classOrObject): bool $this->assertSame('custom message foo', $violations->get(0)->getMessage()); $this->assertSame('This value should satisfy at least one of the following constraints: [1] custom message bar', $violations->get(1)->getMessage()); } + + public function testNestedConstraintsAreNotExecutedWhenGroupDoesNotMatch() + { + $validator = Validation::createValidator(); + + $violations = $validator->validate(50, new AtLeastOneOf([ + 'constraints' => [ + new Range([ + 'groups' => 'adult', + 'min' => 18, + 'max' => 55, + ]), + new GreaterThan([ + 'groups' => 'senior', + 'value' => 55, + ]), + ], + 'groups' => ['adult', 'senior'], + ]), 'senior'); + + $this->assertCount(1, $violations); + } } class ExpressionConstraintNested diff --git a/src/Symfony/Component/Validator/Tests/Constraints/SequentiallyValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/SequentiallyValidatorTest.php index be11448ad28e4..1dca3ccd1c186 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/SequentiallyValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/SequentiallyValidatorTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Tests\Constraints; +use Symfony\Component\Validator\Constraints\GreaterThan; use Symfony\Component\Validator\Constraints\NotEqualTo; use Symfony\Component\Validator\Constraints\Range; use Symfony\Component\Validator\Constraints\Regex; @@ -19,6 +20,7 @@ use Symfony\Component\Validator\Constraints\Type; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; +use Symfony\Component\Validator\Validation; class SequentiallyValidatorTest extends ConstraintValidatorTestCase { @@ -61,4 +63,26 @@ public function testStopsAtFirstConstraintWithViolations() $this->assertCount(1, $this->context->getViolations()); } + + public function testNestedConstraintsAreNotExecutedWhenGroupDoesNotMatch() + { + $validator = Validation::createValidator(); + + $violations = $validator->validate(50, new Sequentially([ + 'constraints' => [ + new GreaterThan([ + 'groups' => 'senior', + 'value' => 55, + ]), + new Range([ + 'groups' => 'adult', + 'min' => 18, + 'max' => 55, + ]), + ], + 'groups' => ['adult', 'senior'], + ]), 'adult'); + + $this->assertCount(0, $violations); + } }