Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c1b1ba

Browse files
bug #34464 [Form] group constraints when calling the validator (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [Form] group constraints when calling the validator | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Follow up of #34081 Spotted during the workshop at SymfonyCon, while trying to fix deprecation notices on symfony-demo: the Form component currently passes constraints one by one for validation, effectively preventing the validator from taking care of cross-constraints dependencies. This PR fixes it. This will prevent ppl from having to fix things like > Using the "Symfony\Component\Validator\Constraints\Length" constraint with the "min" option without setting the "allowEmptyString" one is deprecated and defaults to true. In 5.0, it will become optional and default to false. Commits ------- d15f77f [Form] group constraints when calling the validator
2 parents 698cc6b + d15f77f commit 2c1b1ba

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public function validate($form, Constraint $formConstraint)
7676
}
7777
}
7878
} else {
79+
$groupedConstraints = [];
80+
7981
foreach ($constraints as $constraint) {
8082
// For the "Valid" constraint, validate the data in all groups
8183
if ($constraint instanceof Valid) {
@@ -88,7 +90,7 @@ public function validate($form, Constraint $formConstraint)
8890
// matching group
8991
foreach ($groups as $group) {
9092
if (\in_array($group, $constraint->groups)) {
91-
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
93+
$groupedConstraints[$group][] = $constraint;
9294

9395
// Prevent duplicate validation
9496
if (!$constraint instanceof Composite) {
@@ -97,6 +99,10 @@ public function validate($form, Constraint $formConstraint)
9799
}
98100
}
99101
}
102+
103+
foreach ($groupedConstraints as $group => $constraint) {
104+
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
105+
}
100106
}
101107
} elseif (!$form->isSynchronized()) {
102108
$childrenSynchronized = true;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\Translation\IdentityTranslator;
2727
use Symfony\Component\Validator\Constraints\Collection;
2828
use Symfony\Component\Validator\Constraints\GroupSequence;
29+
use Symfony\Component\Validator\Constraints\Length;
2930
use Symfony\Component\Validator\Constraints\NotBlank;
3031
use Symfony\Component\Validator\Constraints\NotNull;
3132
use Symfony\Component\Validator\Constraints\Valid;
@@ -77,10 +78,11 @@ public function testValidateConstraints()
7778
$object = new \stdClass();
7879
$constraint1 = new NotNull(['groups' => ['group1', 'group2']]);
7980
$constraint2 = new NotBlank(['groups' => 'group2']);
81+
$constraint3 = new Length(['groups' => 'group2', 'min' => 3]);
8082

8183
$options = [
8284
'validation_groups' => ['group1', 'group2'],
83-
'constraints' => [$constraint1, $constraint2],
85+
'constraints' => [$constraint1, $constraint2, $constraint3],
8486
];
8587
$form = $this->getCompoundForm($object, $options);
8688
$form->submit([]);
@@ -89,8 +91,8 @@ public function testValidateConstraints()
8991
$this->expectValidateAt(0, 'data', $object, ['group1', 'group2']);
9092

9193
// Then custom constraints
92-
$this->expectValidateValueAt(1, 'data', $object, $constraint1, 'group1');
93-
$this->expectValidateValueAt(2, 'data', $object, $constraint2, 'group2');
94+
$this->expectValidateValueAt(1, 'data', $object, [$constraint1], 'group1');
95+
$this->expectValidateValueAt(2, 'data', $object, [$constraint2, $constraint3], 'group2');
9496

9597
$this->validator->validate($form, new Form());
9698

@@ -172,8 +174,8 @@ public function testValidateConstraintsOptionEvenIfNoValidConstraint()
172174
$parent->add($form);
173175
$parent->submit([]);
174176

175-
$this->expectValidateValueAt(0, 'data', $object, $constraint1, 'group1');
176-
$this->expectValidateValueAt(1, 'data', $object, $constraint2, 'group2');
177+
$this->expectValidateValueAt(0, 'data', $object, [$constraint1], 'group1');
178+
$this->expectValidateValueAt(1, 'data', $object, [$constraint2], 'group2');
177179

178180
$this->validator->validate($form, new Form());
179181

0 commit comments

Comments
 (0)