diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index 54dafd3be7..2abf92f9c1 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -11,7 +11,11 @@ namespace Symfony\Component\Validator; -/* +/** + * Base class for constraint validators + * + * @author Bernhard Schussek + * * @api */ abstract class ConstraintValidator implements ConstraintValidatorInterface diff --git a/src/Symfony/Component/Validator/ConstraintViolationList.php b/src/Symfony/Component/Validator/ConstraintViolationList.php index baeaef224b..808a69a182 100644 --- a/src/Symfony/Component/Validator/ConstraintViolationList.php +++ b/src/Symfony/Component/Validator/ConstraintViolationList.php @@ -12,14 +12,33 @@ namespace Symfony\Component\Validator; /** - * An array-acting object that holds many ConstrainViolation instances. + * An list of ConstrainViolation objects. + * + * @author Bernhard Schussek * * @api */ class ConstraintViolationList implements \IteratorAggregate, \Countable, \ArrayAccess { + /** + * The constraint violations + * + * @var array + */ protected $violations = array(); + /** + * Creates a new constraint violation list + * + * @param array $violations The constraint violations to add to the list + */ + public function __construct(array $violations = array()) + { + foreach ($violations as $violation) { + $this->add($violation); + } + } + /** * @return string */ diff --git a/src/Symfony/Component/Validator/GraphWalker.php b/src/Symfony/Component/Validator/GraphWalker.php index 0acd64d6cf..a88eb8b3a7 100644 --- a/src/Symfony/Component/Validator/GraphWalker.php +++ b/src/Symfony/Component/Validator/GraphWalker.php @@ -174,16 +174,20 @@ 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); + $messageTemplate = $validator->getMessageTemplate(); + $messageParams = $validator->getMessageParameters(); - $this->context->addViolation( - $validator->getMessageTemplate(), - $validator->getMessageParameters(), - $value - ); + // Somewhat ugly hack: Don't add a violation if no message is set. + // This is required if the validator added its violations directly + // to the context already + if (!empty($messageTemplate)) { + // 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($messageTemplate, $messageParams, $value); + } } } } diff --git a/tests/Symfony/Tests/Component/Validator/ExecutionContextTest.php b/tests/Symfony/Tests/Component/Validator/ExecutionContextTest.php index 1b47bdf268..a4410b0f0c 100644 --- a/tests/Symfony/Tests/Component/Validator/ExecutionContextTest.php +++ b/tests/Symfony/Tests/Component/Validator/ExecutionContextTest.php @@ -98,9 +98,9 @@ class ExecutionContextTest extends \PHPUnit_Framework_TestCase $violations = $this->context->getViolations(); $expected = <<add(new ConstraintViolation( - '', + 'Failed', array(), 'Root', 'firstName', @@ -175,7 +175,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase // "Default" was launched $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), 'Root', 'reference', @@ -202,7 +202,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase // Only group "Second" was validated $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), 'Root', 'lastName', @@ -254,7 +254,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), 'Root', 'path', @@ -286,7 +286,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), 'Root', 'path[key]', @@ -319,7 +319,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), 'Root', 'path[key]', diff --git a/tests/Symfony/Tests/Component/Validator/ValidatorTest.php b/tests/Symfony/Tests/Component/Validator/ValidatorTest.php index c161a49970..b07e596a10 100644 --- a/tests/Symfony/Tests/Component/Validator/ValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/ValidatorTest.php @@ -55,7 +55,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase // Only the constraint of group "Default" failed $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), $entity, 'firstName', @@ -78,7 +78,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase // Only the constraint of group "Custom" failed $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), $entity, 'lastName', @@ -103,14 +103,14 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase // The constraints of both groups failed $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), $entity, 'firstName', '' )); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), $entity, 'lastName', @@ -150,7 +150,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase { $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation( - '', + 'Failed', array(), '', '',