Skip to content

Commit

Permalink
[Validator] Fixed: GraphWalker does not add constraint violation if e…
Browse files Browse the repository at this point in the history
…rror message is empty
  • Loading branch information
webmozart committed Jan 30, 2012
1 parent 1dd302c commit f904a9e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 26 deletions.
6 changes: 5 additions & 1 deletion src/Symfony/Component/Validator/ConstraintValidator.php
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion src/Symfony/Component/Validator/ConstraintViolationList.php
Expand Up @@ -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
*/
Expand Down
24 changes: 14 additions & 10 deletions src/Symfony/Component/Validator/GraphWalker.php
Expand Up @@ -174,16 +174,20 @@ public function walkConstraint(Constraint $constraint, $value, $group, $property
$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(),
$value
);
$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($messageTemplate, $messageParams, $value);
}
}
}
}
Expand Up @@ -98,9 +98,9 @@ public function testViolationsAsString()
$violations = $this->context->getViolations();

$expected = <<<EOF
Root.:
Root:
Message 1
Root.:
Root:
Message 2
EOF;
Expand Down
Expand Up @@ -6,7 +6,7 @@

class FailingConstraint extends Constraint
{
public $message = '';
public $message = 'Failed';

public function getTargets()
{
Expand Down
12 changes: 6 additions & 6 deletions tests/Symfony/Tests/Component/Validator/GraphWalkerTest.php
Expand Up @@ -143,7 +143,7 @@ public function testWalkObjectInDefaultGroupTraversesGroupSequence()
// validated
$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
'Failed',
array(),
'Root',
'firstName',
Expand Down Expand Up @@ -175,7 +175,7 @@ public function testWalkObjectInGroupSequencePropagatesDefaultGroup()
// "Default" was launched
$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
'Failed',
array(),
'Root',
'reference',
Expand All @@ -202,7 +202,7 @@ public function testWalkObjectInOtherGroupTraversesNoGroupSequence()
// Only group "Second" was validated
$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
'Failed',
array(),
'Root',
'lastName',
Expand Down Expand Up @@ -254,7 +254,7 @@ public function testWalkCascadedPropertyValidatesReferences()

$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
'Failed',
array(),
'Root',
'path',
Expand Down Expand Up @@ -286,7 +286,7 @@ public function testWalkCascadedPropertyValidatesArraysByDefault()

$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
'Failed',
array(),
'Root',
'path[key]',
Expand Down Expand Up @@ -319,7 +319,7 @@ public function testWalkCascadedPropertyValidatesTraversableByDefault()

$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
'Failed',
array(),
'Root',
'path[key]',
Expand Down
10 changes: 5 additions & 5 deletions tests/Symfony/Tests/Component/Validator/ValidatorTest.php
Expand Up @@ -55,7 +55,7 @@ public function testValidate_defaultGroup()
// Only the constraint of group "Default" failed
$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
'Failed',
array(),
$entity,
'firstName',
Expand All @@ -78,7 +78,7 @@ public function testValidate_oneGroup()
// Only the constraint of group "Custom" failed
$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
'Failed',
array(),
$entity,
'lastName',
Expand All @@ -103,14 +103,14 @@ public function testValidate_multipleGroups()
// 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',
Expand Down Expand Up @@ -150,7 +150,7 @@ public function testValidateValue()
{
$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation(
'',
'Failed',
array(),
'',
'',
Expand Down

0 comments on commit f904a9e

Please sign in to comment.