Permalink
Browse files

bug #24983 [Validator] enter the context in which to validate (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] enter the context in which to validate

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24246
| License       | MIT
| Doc PR        |

Commits
-------

7359cbe [Validator] enter the context in which to validate
  • Loading branch information...
stof committed Nov 17, 2017
2 parents 5766e1d + 7359cbe commit 2f19ddbd47867ec5fdde228e015f44eacedd4299
@@ -26,12 +26,9 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
}
$violations = $this->context->getValidator()->validate($value, null, array($this->context->getGroup()));
foreach ($violations as $violation) {
$this->context->buildViolation($violation->getMessage(), $violation->getParameters())
->atPath($violation->getPropertyPath())
->addViolation();
}
$this->context
->getValidator()
->inContext($this->context)
->validate($value, null, array($this->context->getGroup()));
}
}
@@ -0,0 +1,61 @@
<?php
namespace Symfony\Component\Validator\Tests\Constraints;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\ValidValidator;
use Symfony\Component\Validator\ValidatorBuilder;
class ValidValidatorTest extends TestCase
{
public function testPropertyPathsArePassedToNestedContexts()
{
$validatorBuilder = new ValidatorBuilder();
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
$violations = $validator->validate(new Foo(), null, array('nested'));
$this->assertCount(1, $violations);
$this->assertSame('fooBar.fooBarBaz.foo', $violations->get(0)->getPropertyPath());
}
protected function createValidator()
{
return new ValidValidator();
}
}
class Foo
{
/**
* @Assert\Valid(groups={"nested"})
*/
public $fooBar;
public function __construct()
{
$this->fooBar = new FooBar();
}
}
class FooBar
{
/**
* @Assert\Valid(groups={"nested"})
*/
public $fooBarBaz;
public function __construct()
{
$this->fooBarBaz = new FooBarBaz();
}
}
class FooBarBaz
{
/**
* @Assert\NotBlank(groups={"nested"})
*/
public $foo;
}

0 comments on commit 2f19ddb

Please sign in to comment.