[Validator] enter the context in which to validate
This commit is contained in:
parent
ecad1c4b3b
commit
7359cbe063
@ -26,12 +26,9 @@ class ValidValidator extends ConstraintValidator
|
|||||||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
|
||||||
}
|
}
|
||||||
|
|
||||||
$violations = $this->context->getValidator()->validate($value, null, array($this->context->getGroup()));
|
$this->context
|
||||||
|
->getValidator()
|
||||||
foreach ($violations as $violation) {
|
->inContext($this->context)
|
||||||
$this->context->buildViolation($violation->getMessage(), $violation->getParameters())
|
->validate($value, null, array($this->context->getGroup()));
|
||||||
->atPath($violation->getPropertyPath())
|
|
||||||
->addViolation();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
Reference in New Issue
Block a user