[Validator] enter the context in which to validate

This commit is contained in:
Christian Flothmann 2017-11-15 20:37:49 +01:00
parent ecad1c4b3b
commit 7359cbe063
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;
}