[Form] Prevented duplicate validation of form constraints

This commit is contained in:
Bernhard Schussek 2012-07-09 19:22:35 +02:00
parent b4c55bdcf4
commit c0a520792b
4 changed files with 31 additions and 17 deletions

View File

@ -937,6 +937,24 @@
));
```
Be aware that constraints will now only be validated if they belong
to the validated group! So if you validate a form in group "Custom"
and previously did:
```
$builder->add('name', 'text', array(
'validation_constraint' => new NotBlank(),
));
```
Then you need to add the constraint to the group "Custom" now:
```
$builder->add('name', 'text', array(
'constraints' => new NotBlank(array('groups' => 'Custom')),
));
```
### Validator
* The methods `setMessage()`, `getMessageTemplate()` and

View File

@ -144,3 +144,4 @@ CHANGELOG
* ChoiceType now doesn't add the empty value anymore if the choices already contain an empty element
* DateType, TimeType and DateTimeType now show empty values again if not required
* [BC BREAK] fixed rendering of errors for DateType, BirthdayType and similar ones
* [BC BREAK] fixed: form constraints are only validated if they belong to the validated group

View File

@ -71,7 +71,12 @@ class FormValidator extends ConstraintValidator
$constraints = $config->getOption('constraints');
foreach ($constraints as $constraint) {
foreach ($groups as $group) {
$graphWalker->walkConstraint($constraint, $form->getData(), $group, $path . 'data');
if (in_array($group, $constraint->groups)) {
$graphWalker->walkConstraint($constraint, $form->getData(), $group, $path . 'data');
// Prevent duplicate validation
continue 2;
}
}
}
} else {

View File

@ -19,6 +19,8 @@ use Symfony\Component\Form\Extension\Validator\Constraints\Form;
use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator;
use Symfony\Component\Form\Util\PropertyPath;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\GlobalExecutionContext;
use Symfony\Component\Validator\ExecutionContext;
@ -88,8 +90,8 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
$context = $this->getExecutionContext();
$graphWalker = $context->getGraphWalker();
$object = $this->getMock('\stdClass');
$constraint1 = $this->getMock('Symfony\Component\Validator\Constraint');
$constraint2 = $this->getMock('Symfony\Component\Validator\Constraint');
$constraint1 = new NotNull(array('groups' => array('group1', 'group2')));
$constraint2 = new NotBlank(array('groups' => 'group2'));
$options = array(
'validation_groups' => array('group1', 'group2'),
@ -112,12 +114,6 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
->method('walkConstraint')
->with($constraint1, $object, 'group1', 'data');
$graphWalker->expects($this->at(3))
->method('walkConstraint')
->with($constraint1, $object, 'group2', 'data');
$graphWalker->expects($this->at(4))
->method('walkConstraint')
->with($constraint2, $object, 'group1', 'data');
$graphWalker->expects($this->at(5))
->method('walkConstraint')
->with($constraint2, $object, 'group2', 'data');
@ -153,8 +149,8 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
$context = $this->getExecutionContext();
$graphWalker = $context->getGraphWalker();
$object = $this->getMock('\stdClass');
$constraint1 = $this->getMock('Symfony\Component\Validator\Constraint');
$constraint2 = $this->getMock('Symfony\Component\Validator\Constraint');
$constraint1 = new NotNull(array('groups' => array('group1', 'group2')));
$constraint2 = new NotBlank(array('groups' => 'group2'));
$parent = $this->getBuilder('parent', null, array('cascade_validation' => false))
->setCompound(true)
@ -173,12 +169,6 @@ class FormValidatorTest extends \PHPUnit_Framework_TestCase
->method('walkConstraint')
->with($constraint1, $object, 'group1', 'data');
$graphWalker->expects($this->at(1))
->method('walkConstraint')
->with($constraint1, $object, 'group2', 'data');
$graphWalker->expects($this->at(2))
->method('walkConstraint')
->with($constraint2, $object, 'group1', 'data');
$graphWalker->expects($this->at(3))
->method('walkConstraint')
->with($constraint2, $object, 'group2', 'data');