Skip to content

Commit

Permalink
Merge branch '6.0' into 6.1
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fabpot committed Aug 12, 2022
2 parents 2f717ce + 0987eb0 commit 14ec426
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ CHANGELOG
5.1.0
-----

* Add `AtLeastOneOf` constraint that is considered to be valid if at least one of the nested constraints is valid
* added the `Hostname` constraint and validator
* added the `alpha3` option to the `Country` and `Language` constraints
* allow to define a reusable set of constraints by extending the `Compound` constraint
Expand Down
4 changes: 4 additions & 0 deletions Constraints/AtLeastOneOfValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function validate(mixed $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();
Expand Down
23 changes: 23 additions & 0 deletions Tests/Constraints/AtLeastOneOfValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions Tests/Constraints/SequentiallyValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 14ec426

Please sign in to comment.