Skip to content

Commit

Permalink
bug #25297 [Validator] Fixed the @Valid(groups={"group"}) against nul…
Browse files Browse the repository at this point in the history
…l 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.
  • Loading branch information
nicolas-grekas committed Dec 4, 2017
2 parents 8c27dd4 + 56f24d0 commit 86b0598
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Expand Up @@ -26,6 +26,10 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
}

if (null === $value) {
return;
}

$this->context
->getValidator()
->inContext($this->context)
Expand Down
Expand Up @@ -20,6 +20,18 @@ public function testPropertyPathsArePassedToNestedContexts()
$this->assertSame('fooBar.fooBarBaz.foo', $violations->get(0)->getPropertyPath());
}

public function testNullValues()
{
$validatorBuilder = new ValidatorBuilder();
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();

$foo = new Foo();
$foo->fooBar = null;
$violations = $validator->validate($foo, null, array('nested'));

$this->assertCount(0, $violations);
}

protected function createValidator()
{
return new ValidValidator();
Expand Down

0 comments on commit 86b0598

Please sign in to comment.