Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
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
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);
}
}