Skip to content

Commit 14ec426

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: [String] Add tests for AsciiSlugger [Serializer] Fix get accessor regex in AnnotationLoader [HttpKernel] Fix passing `null` to `\trim()` method in LoggerDataCollector Remove wrong PHPDoc [Translation] Crowdin provider throw Exception when status is 50x Always attempt to listen for notifications add missing changelog entry for the AtLeastOneOf constraint validate nested constraints only if they are in the same group
2 parents 2f717ce + 0987eb0 commit 14ec426

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ CHANGELOG
7070
5.1.0
7171
-----
7272

73+
* Add `AtLeastOneOf` constraint that is considered to be valid if at least one of the nested constraints is valid
7374
* added the `Hostname` constraint and validator
7475
* added the `alpha3` option to the `Country` and `Language` constraints
7576
* allow to define a reusable set of constraints by extending the `Compound` constraint

Constraints/AtLeastOneOfValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function validate(mixed $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();

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

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)