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
-------

7359cbe063 [Validator] enter the context in which to validate
This commit is contained in:
Christophe Coevoet 2017-11-17 15:23:59 +01:00
commit 2f19ddbd47
2 changed files with 65 additions and 7 deletions

View File

@ -26,12 +26,9 @@ class ValidValidator extends ConstraintValidator
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()));
}
}

View File

@ -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;
}