Skip to content

Commit 86b0598

Browse files
bug symfony#25297 [Validator] Fixed the @Valid(groups={"group"}) against null exception case (vudaltsov)
This PR was merged into the 3.4 branch. Discussion ---------- [Validator] Fixed the @Valid(groups={"group"}) against null exception case | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | When `@Valid(groups={"group"})` has non-empty groups and the value is `null`, validator throws `Cannot validate values of type "NULL" automatically. Please provide a constraint.` at `RecursiveContextualValidator:164`. I don't really understand, why everything is okay for `@Valid()` without groups, but hope that my fix is correct anyway. Commits ------- 56f24d0 Fixed the null value exception case.
2 parents 8c27dd4 + 56f24d0 commit 86b0598

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function validate($value, Constraint $constraint)
2626
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
2727
}
2828

29+
if (null === $value) {
30+
return;
31+
}
32+
2933
$this->context
3034
->getValidator()
3135
->inContext($this->context)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ public function testPropertyPathsArePassedToNestedContexts()
2020
$this->assertSame('fooBar.fooBarBaz.foo', $violations->get(0)->getPropertyPath());
2121
}
2222

23+
public function testNullValues()
24+
{
25+
$validatorBuilder = new ValidatorBuilder();
26+
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
27+
28+
$foo = new Foo();
29+
$foo->fooBar = null;
30+
$violations = $validator->validate($foo, null, array('nested'));
31+
32+
$this->assertCount(0, $violations);
33+
}
34+
2335
protected function createValidator()
2436
{
2537
return new ValidValidator();

0 commit comments

Comments
 (0)