8000 [Form] Prevented duplicate validation of form constraints · symfony/symfony@c0a5207 · GitHub
[go: up one dir, main page]

Skip to content

Commit c0a5207

Browse files
committed
[Form] Prevented duplicate validation of form constraints
1 parent b4c55bd commit c0a5207

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

UPGRADE-2.1.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,24 @@
937937
));
938938
```
939939
940+
Be aware that constraints will now only be validated if they belong
941+
to the validated group! So if you validate a form in group "Custom"
942+
and previously did:
943+
944+
```
945+
$builder->add('name 10000 ', 'text', array(
946+
'validation_constraint' => new NotBlank(),
947+
));
948+
```
949+
950+
Then you need to add the constraint to the group "Custom" now:
951+
952+
```
953+
$builder->add('name', 'text', array(
954+
'constraints' => new NotBlank(array('groups' => 'Custom')),
955+
));
956+
```
957+
940958
### Validator
941959
942960
* The methods `setMessage()`, `getMessageTemplate()` and

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,4 @@ CHANGELOG
144144
* ChoiceType now doesn't add the empty value anymore if the choices already contain an empty element
145145
* DateType, TimeType and DateTimeType now show empty values again if not required
146146
* [BC BREAK] fixed rendering of errors for DateType, BirthdayType and similar ones
147+
* [BC BREAK] fixed: form constraints are only validated if they belong to the validated group

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ public function validate($form, Constraint $constraint)
7171
$constraints = $config->getOption('constraints');
7272
foreach ($constraints as $constraint) {
7373
foreach ($groups as $group) {
74-
$graphWalker->walkConstraint($constraint, $form->getData(), $group, $path . 'data');
74+
if (in_array($group, $constraint->groups)) {
75+
$graphWalker->walkConstraint($constraint, $form->getData(), $group, $path . 'data');
76+
77+
// Prevent duplicate validation
78+
continue 2;
79+
}
7580
}
7681
}
7782
} else {

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator;
2020
use Symfony\Component\Form\Util\PropertyPath;
2121
use Symfony\Component\Validator\Constraint;
22+
use Symfony\Component\Validator\Constraints\NotNull;
23+
use Symfony\Component\Validator\Constraints\NotBlank;
2224
use Symfony\Component\Validator\GlobalExecutionContext;
2325
use Symfony\Component\Validator\ExecutionContext;
2426

@@ -88,8 +90,8 @@ public function testValidateConstraints()
8890
$context = $this->getExecutionContext();
8991
$graphWalker = $context->getGraphWalker();
9092
$object = $this->getMock('\stdClass');
91-
$constraint1 = $this->getMock('Symfony\Component\Validator\Constraint');
92-
$constraint2 = $this->getMock('Symfony\Component\Validator\Constraint');
93+
$constraint1 = new NotNull(array('groups' => array('group1', 'group2')));
94+
$constraint2 = new NotBlank(array('groups' => 'group2'));
9395

9496
$options = array(
9597
'validation_groups' => array('group1', 'group2'),
@@ -112,12 +114,6 @@ public function testValidateConstraints()
112114
->method('walkConstraint')
113115
->with($constraint1, $object, 'group1', 'data');
114116
$graphWalker->expects($this->at(3))
115-
->method('walkConstraint')
116-
->with($constraint1, $object, 'group2', 'data');
117-
$graphWalker->expects($this->at(4))
118-
->method('walkConstraint')
119-
->with($constraint2, $object, 'group1', 'data');
120-
$graphWalker->expects($this->at(5))
121117
->method('walkConstraint')
122118
->with($constraint2, $object, 'group2', 'data');
123119

@@ -153,8 +149,8 @@ public function testValidateConstraintsEvenIfNoCascadeValidation()
153149
$context = $this->getExecutionContext();
154150
$graphWalker = $context->getGraphWalker();
155151
$object = $this->get B435 Mock('\stdClass');
156-
$constraint1 = $this->getMock('Symfony\Component\Validator\Constraint');
157-
$constraint2 = $this->getMock('Symfony\Component\Validator\Constraint');
152+
$constraint1 = new NotNull(array('groups' => array('group1', 'group2')));
153+
$constraint2 = new NotBlank(array('groups' => 'group2'));
158154

159155
$parent = $this->getBuilder('parent', null, array('cascade_validation' => false))
160156
->setCompound(true)
@@ -173,12 +169,6 @@ public function testValidateConstraintsEvenIfNoCascadeValidation()
173169
->method('walkConstraint')
174170
->with($constraint1, $object, 'group1', 'data');
175171
$graphWalker->expects($this->at(1))
176-
->method('walkConstraint')
177-
->with($constraint1, $object, 'group2', 'data');
178-
$graphWalker->expects($this->at(2))
179-
->method('walkConstraint')
180-
->with($constraint2, $object, 'group1', 'data');
181-
$graphWalker->expects($this->at(3))
182172
->method('walkConstraint')
183173
->with($constraint2, $object, 'group2', 'data');
184174

0 commit comments

Comments
 (0)
0