Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e3543ba

Browse filesBrowse files
committed
validate nested constraints only if they are in the same group
1 parent 0ad8230 commit e3543ba
Copy full SHA for e3543ba

File tree

2 files changed

+27
-0
lines changed
Filter options

2 files changed

+27
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/AtLeastOneOfValidator.php
+4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php
+23Lines 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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.