8000 validate nested constraints only if they are in the same group · symfony/symfony@2d65298 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d65298

Browse files
committed
validate nested constraints only if they are in the same group
1 parent 458b294 commit 2d65298

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/Symfony/Component/Validator/Constraints/AtLeastOneOfValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function validate($value, Constraint $constraint)
3434
$messages = [$constraint->message];
3535

3636
foreach ($constraint->constraints as $key => $item) {
37+
if (!in_array($this->context->getGroup(), $item->groups, true)) {
38+
continue;
39+
}
40+
3741
$executionContext = clone $this->context;
3842
$executionContext->setNode($value, $this->context->getObject(), $this->context->getMetadata(), $this->context->getPropertyPath());
3943
$violations = $validator->inContext($executionContext)->validate($value, $item, $this->context->getGroup())->getViolations();

src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Validator\Constraints\DivisibleBy;
2020
use Symfony\Component\Validator\Constraints\EqualTo;
2121
use Symfony\Component\Validator\Constraints\Expression;
22+
use Symfony\Component\Validator\Constraints\GreaterThan;
2223
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
2324
use Symfony\Component\Validator\Constraints\IdenticalTo;
2425
use Symfony\Component\Validator\Constraints\Language;
@@ -235,6 +236,28 @@ public function hasMetadataFor($classOrObject): bool
235236
$this->assertSame('custom message foo', $violations->get(0)->getMessage());
236237
$this->assertSame('This value should satisfy at least one of the following constraints: [1] custom message bar', $violations->get(1)->getMessage());
237238
}
239+
240+
public function testNestedConstraintsAreNotExecutedWhenGroupDoesNotMatch()
241+
{
242+
$validator = Validation::createValidator();
243+
244+
$violations = $validator->validate(50, new AtLeastOneOf([
245+
'constraints' => [
246+
new Range([
247+
'groups' => 'adult',
248+
'min' => 18,
249+
'max' => 55,
250+
]),
251+
new GreaterThan([
252+
'groups' => 'senior',
253+
'value' => 55,
254+
]),
255+
],
256+
'groups' => ['adult', 'senior'],
257+
]), 'senior');
258+
259+
$this->assertCount(1, $violations);
260+
}
238261
}
239262

240263
class ExpressionConstraintNested

src/Symfony/Component/Validator/Tests/Constraints/SequentiallyValidatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\Validator\Constraints\GreaterThan;
1415
use Symfony\Component\Validator\Constraints\NotEqualTo;
1516
use Symfony\Component\Validator\Constraints\Range;
1617
use Symfony\Component\Validator\Constraints\Regex;
@@ -19,6 +20,7 @@
1920
use Symfony\Component\Validator\Constraints\Type;
2021
use Symfony\Component\Validator\ConstraintViolation;
2122
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
23+
use Symfony\Component\Validator\Validation;
2224

2325
class SequentiallyValidatorTest extends ConstraintValidatorTestCase
2426
{
@@ -61,4 +63,26 @@ public function testStopsAtFirstConstraintWithViolations()
6163

6264
$this->assertCount(1, $this->context->getViolations());
6365
}
66+
67+
public function testNestedConstraintsAreNotExecutedWhenGroupDoesNotMatch()
68+
{
69+
$validator = Validation::createValidator();
70+
71+
$violations = $validator->validate(50, new Sequentially([
72+
'constraints' => [
73+
new GreaterThan([
74+
'groups' => 'senior',
75+
'value' => 55,
76+
]),
77+
new Range([
78+
'groups' => 'adult',
79+
'min' => 18,
80+
'max' => 55,
81+
]),
82+
],
83+
'groups' => ['adult', 'senior'],
84+
]), 'adult');
85+
86+
$this->assertCount(0, $violations);
87+
}
6488
}

0 commit comments

Comments
 (0)
0