[Validator] Fixed: GraphWalker does not add constraint violation if error message is empty
This commit is contained in:
parent
1dd302cbea
commit
f904a9ed53
|
@ -11,7 +11,11 @@
|
|||
|
||||
namespace Symfony\Component\Validator;
|
||||
|
||||
/*
|
||||
/**
|
||||
* Base class for constraint validators
|
||||
*
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
abstract class ConstraintValidator implements ConstraintValidatorInterface
|
||||
|
|
|
@ -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 <bschussek@gmail.com>
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
|
|
|
@ -174,16 +174,20 @@ class GraphWalker
|
|||
$validator->initialize($this->context);
|
||||
|
||||
if (!$validator->isValid($value, $constraint)) {
|
||||
$messageTemplate = $validator->getMessageTemplate();
|
||||
$messageParams = $validator->getMessageParameters();
|
||||
|
||||
// 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(
|
||||
$validator->getMessageTemplate(),
|
||||
$validator->getMessageParameters(),
|
||||
$value
|
||||
);
|
||||
$this->context->addViolation($messageTemplate, $messageParams, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,9 +98,9 @@ class ExecutionContextTest extends \PHPUnit_Framework_TestCase
|
|||
$violations = $this->context->getViolations();
|
||||
|
||||
$expected = <<<EOF
|
||||
Root.:
|
||||
Root:
|
||||
Message 1
|
||||
Root.:
|
||||
Root:
|
||||
Message 2
|
||||
|
||||
EOF;
|
||||
|
|
|
@ -6,7 +6,7 @@ use Symfony\Component\Validator\Constraint;
|
|||
|
||||
class FailingConstraint extends Constraint
|
||||
{
|
||||
public $message = '';
|
||||
public $message = 'Failed';
|
||||
|
||||
public function getTargets()
|
||||
{
|
||||
|
|
|
@ -143,7 +143,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase
|
|||
// validated
|
||||
$violations = new ConstraintViolationList();
|
||||
$violations->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]',
|
||||
|
|
|
@ -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(),
|
||||
'',
|
||||
'',
|
||||
|
|
Reference in New Issue