[Validator] fixed error message property path when a collection error occurs
This commit is contained in:
parent
1238bb3d53
commit
c04512086e
@ -175,6 +175,11 @@ class GraphWalker
|
||||
$validator->initialize($this->context);
|
||||
|
||||
if (!$validator->isValid($value, $constraint)) {
|
||||
// Resetting the property path. This is needed because some
|
||||
// validators, like CollectionValidator, use the walker internally
|
||||
// and so change the context.
|
||||
$this->context->setPropertyPath($propertyPath);
|
||||
|
||||
$this->context->addViolation(
|
||||
$validator->getMessageTemplate(),
|
||||
$validator->getMessageParameters(),
|
||||
|
@ -30,6 +30,7 @@ use Symfony\Component\Validator\ConstraintViolationList;
|
||||
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\Constraints\Collection;
|
||||
|
||||
class GraphWalkerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -411,4 +412,30 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals(0, count($this->walker->getViolations()));
|
||||
}
|
||||
|
||||
public function testWalkObjectUsesCorrectPropertyPathInViolationsWhenUsingCollections()
|
||||
{
|
||||
$constraint = new Collection(array(
|
||||
'foo' => new ConstraintA(),
|
||||
'bar' => new ConstraintA(),
|
||||
));
|
||||
|
||||
$this->walker->walkConstraint($constraint, array('foo' => 'VALID'), 'Default', 'collection');
|
||||
$violations = $this->walker->getViolations();
|
||||
$this->assertEquals('collection', $violations[0]->getPropertyPath());
|
||||
}
|
||||
|
||||
public function testWalkObjectUsesCorrectPropertyPathInViolationsWhenUsingNestedCollections()
|
||||
{
|
||||
$constraint = new Collection(array(
|
||||
'foo' => new Collection(array(
|
||||
'foo' => new ConstraintA(),
|
||||
'bar' => new ConstraintA(),
|
||||
)),
|
||||
));
|
||||
|
||||
$this->walker->walkConstraint($constraint, array('foo' => array('foo' => 'VALID')), 'Default', 'collection');
|
||||
$violations = $this->walker->getViolations();
|
||||
$this->assertEquals('collection[foo]', $violations[0]->getPropertyPath());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user