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

[Validator] validate nested constraints only if they are in the same group #47211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2022
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);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.