Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c582b76

Browse files
committed
bug #57925 [Validator] reset the validation context after validating nested constraints (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [Validator] reset the validation context after validating nested constraints | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #54577 | License | MIT Commits ------- 874e3f9 reset the validation context after validating nested constraints
2 parents 6b4a3a7 + 874e3f9 commit c582b76

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ public function validate($value, Constraint $constraint)
4242
continue;
4343
}
4444

45+
$context = $this->context;
4546
$executionContext = clone $this->context;
4647
$executionContext->setNode($value, $this->context->getObject(), $this->context->getMetadata(), $this->context->getPropertyPath());
4748
$violations = $validator->inContext($executionContext)->validate($value, $item, $this->context->getGroup())->getViolations();
49+
$this->context = $context;
4850

4951
if (\count($this->context->getViolations()) === \count($violations)) {
5052
return;

src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
1515
use Symfony\Component\Validator\Constraints\AtLeastOneOfValidator;
1616
use Symfony\Component\Validator\Constraints\Choice;
17+
use Symfony\Component\Validator\Constraints\Collection;
1718
use Symfony\Component\Validator\Constraints\Count;
1819
use Symfony\Component\Validator\Constraints\Country;
1920
use Symfony\Component\Validator\Constraints\DivisibleBy;
@@ -27,9 +28,11 @@
2728
use Symfony\Component\Validator\Constraints\Length;
2829
use Symfony\Component\Validator\Constraints\LessThan;
2930
use Symfony\Component\Validator\Constraints\Negative;
31+
use Symfony\Component\Validator\Constraints\NotBlank;
3032
use Symfony\Component\Validator\Constraints\NotNull;
3133
use Symfony\Component\Validator\Constraints\Range;
3234
use Symfony\Component\Validator\Constraints\Regex;
35+
use Symfony\Component\Validator\Constraints\Type;
3336
use Symfony\Component\Validator\Constraints\Unique;
3437
use Symfony\Component\Validator\Constraints\Valid;
3538
use Symfony\Component\Validator\ConstraintViolation;
@@ -296,6 +299,35 @@ public function trans(?string $id, array $parameters = [], ?string $domain = nul
296299
$this->assertCount(1, $violations);
297300
$this->assertSame('Dummy translation: [1] Dummy violation.', $violations->get(0)->getMessage());
298301
}
302+
303+
public function testValidateNestedAtLeaseOneOfConstraints()
304+
{
305+
$data = [
306+
'foo' => [
307+
'bar' => 'foo.bar',
308+
'baz' => 'foo.baz',
309+
],
310+
];
311+
312+
$constraints = new Collection([
313+
'foo' => new AtLeastOneOf([
314+
new Collection([
315+
'bar' => new AtLeastOneOf([
316+
new Type('int'),
317+
new Choice(['test1', 'test2'])
318+
]),
319+
]),
320+
new Collection([
321+
'baz' => new Type('int'),
322+
]),
323+
]),
324+
]);
325+
326+
$validator = Validation::createValidator();
327+
$violations = $validator->validate($data, $constraints);
328+
329+
self::assertCount(1, $violations);
330+
}
299331
}
300332

301333
class ExpressionConstraintNested

0 commit comments

Comments
 (0)