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

Commit 86b0598

Browse filesBrowse files
bug #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
Copy full SHA for 86b0598

File tree

2 files changed

+16
-0
lines changed
Filter options

2 files changed

+16
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/ValidValidator.php
+4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/ValidValidatorTest.php
+12Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.