[Validator] fixed error message property path when a collection error occurs

This commit is contained in:
Fabien Potencier 2011-07-16 00:22:57 +02:00
parent 1238bb3d53
commit c04512086e
2 changed files with 32 additions and 0 deletions

View File

@ -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(),

View File

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