[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;
|
namespace Symfony\Component\Validator;
|
||||||
|
|
||||||
/*
|
/**
|
||||||
|
* Base class for constraint validators
|
||||||
|
*
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
abstract class ConstraintValidator implements ConstraintValidatorInterface
|
abstract class ConstraintValidator implements ConstraintValidatorInterface
|
||||||
|
@ -12,14 +12,33 @@
|
|||||||
namespace Symfony\Component\Validator;
|
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
|
* @api
|
||||||
*/
|
*/
|
||||||
class ConstraintViolationList implements \IteratorAggregate, \Countable, \ArrayAccess
|
class ConstraintViolationList implements \IteratorAggregate, \Countable, \ArrayAccess
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* The constraint violations
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
protected $violations = 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
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
@ -174,16 +174,20 @@ class GraphWalker
|
|||||||
$validator->initialize($this->context);
|
$validator->initialize($this->context);
|
||||||
|
|
||||||
if (!$validator->isValid($value, $constraint)) {
|
if (!$validator->isValid($value, $constraint)) {
|
||||||
// Resetting the property path. This is needed because some
|
$messageTemplate = $validator->getMessageTemplate();
|
||||||
// validators, like CollectionValidator, use the walker internally
|
$messageParams = $validator->getMessageParameters();
|
||||||
// and so change the context.
|
|
||||||
$this->context->setPropertyPath($propertyPath);
|
|
||||||
|
|
||||||
$this->context->addViolation(
|
// Somewhat ugly hack: Don't add a violation if no message is set.
|
||||||
$validator->getMessageTemplate(),
|
// This is required if the validator added its violations directly
|
||||||
$validator->getMessageParameters(),
|
// to the context already
|
||||||
$value
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,9 +98,9 @@ class ExecutionContextTest extends \PHPUnit_Framework_TestCase
|
|||||||
$violations = $this->context->getViolations();
|
$violations = $this->context->getViolations();
|
||||||
|
|
||||||
$expected = <<<EOF
|
$expected = <<<EOF
|
||||||
Root.:
|
Root:
|
||||||
Message 1
|
Message 1
|
||||||
Root.:
|
Root:
|
||||||
Message 2
|
Message 2
|
||||||
|
|
||||||
EOF;
|
EOF;
|
||||||
|
@ -6,7 +6,7 @@ use Symfony\Component\Validator\Constraint;
|
|||||||
|
|
||||||
class FailingConstraint extends Constraint
|
class FailingConstraint extends Constraint
|
||||||
{
|
{
|
||||||
public $message = '';
|
public $message = 'Failed';
|
||||||
|
|
||||||
public function getTargets()
|
public function getTargets()
|
||||||
{
|
{
|
||||||
|
@ -143,7 +143,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase
|
|||||||
// validated
|
// validated
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
'Root',
|
'Root',
|
||||||
'firstName',
|
'firstName',
|
||||||
@ -175,7 +175,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase
|
|||||||
// "Default" was launched
|
// "Default" was launched
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
'Root',
|
'Root',
|
||||||
'reference',
|
'reference',
|
||||||
@ -202,7 +202,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase
|
|||||||
// Only group "Second" was validated
|
// Only group "Second" was validated
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
'Root',
|
'Root',
|
||||||
'lastName',
|
'lastName',
|
||||||
@ -254,7 +254,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
'Root',
|
'Root',
|
||||||
'path',
|
'path',
|
||||||
@ -286,7 +286,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
'Root',
|
'Root',
|
||||||
'path[key]',
|
'path[key]',
|
||||||
@ -319,7 +319,7 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
'Root',
|
'Root',
|
||||||
'path[key]',
|
'path[key]',
|
||||||
|
@ -55,7 +55,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
// Only the constraint of group "Default" failed
|
// Only the constraint of group "Default" failed
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
$entity,
|
$entity,
|
||||||
'firstName',
|
'firstName',
|
||||||
@ -78,7 +78,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
// Only the constraint of group "Custom" failed
|
// Only the constraint of group "Custom" failed
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
$entity,
|
$entity,
|
||||||
'lastName',
|
'lastName',
|
||||||
@ -103,14 +103,14 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
// The constraints of both groups failed
|
// The constraints of both groups failed
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
$entity,
|
$entity,
|
||||||
'firstName',
|
'firstName',
|
||||||
''
|
''
|
||||||
));
|
));
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
$entity,
|
$entity,
|
||||||
'lastName',
|
'lastName',
|
||||||
@ -150,7 +150,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$violations = new ConstraintViolationList();
|
$violations = new ConstraintViolationList();
|
||||||
$violations->add(new ConstraintViolation(
|
$violations->add(new ConstraintViolation(
|
||||||
'',
|
'Failed',
|
||||||
array(),
|
array(),
|
||||||
'',
|
'',
|
||||||
'',
|
'',
|
||||||
|
Reference in New Issue
Block a user