Skip to content

Commit

Permalink
bug #34464 [Form] group constraints when calling the validator (nicol…
Browse files Browse the repository at this point in the history
…as-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
  • Loading branch information
nicolas-grekas committed Nov 21, 2019
2 parents 698cc6b + d15f77f commit 2c1b1ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Expand Up @@ -76,6 +76,8 @@ public function validate($form, Constraint $formConstraint)
}
}
} else {
$groupedConstraints = [];

foreach ($constraints as $constraint) {
// For the "Valid" constraint, validate the data in all groups
if ($constraint instanceof Valid) {
Expand All @@ -88,7 +90,7 @@ public function validate($form, Constraint $formConstraint)
// matching group
foreach ($groups as $group) {
if (\in_array($group, $constraint->groups)) {
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
$groupedConstraints[$group][] = $constraint;

// Prevent duplicate validation
if (!$constraint instanceof Composite) {
Expand All @@ -97,6 +99,10 @@ public function validate($form, Constraint $formConstraint)
}
}
}

foreach ($groupedConstraints as $group => $constraint) {
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
}
}
} elseif (!$form->isSynchronized()) {
$childrenSynchronized = true;
Expand Down
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Valid;
Expand Down Expand Up @@ -77,10 +78,11 @@ public function testValidateConstraints()
$object = new \stdClass();
$constraint1 = new NotNull(['groups' => ['group1', 'group2']]);
$constraint2 = new NotBlank(['groups' => 'group2']);
$constraint3 = new Length(['groups' => 'group2', 'min' => 3]);

$options = [
'validation_groups' => ['group1', 'group2'],
'constraints' => [$constraint1, $constraint2],
'constraints' => [$constraint1, $constraint2, $constraint3],
];
$form = $this->getCompoundForm($object, $options);
$form->submit([]);
Expand All @@ -89,8 +91,8 @@ public function testValidateConstraints()
$this->expectValidateAt(0, 'data', $object, ['group1', 'group2']);

// Then custom constraints
$this->expectValidateValueAt(1, 'data', $object, $constraint1, 'group1');
$this->expectValidateValueAt(2, 'data', $object, $constraint2, 'group2');
$this->expectValidateValueAt(1, 'data', $object, [$constraint1], 'group1');
$this->expectValidateValueAt(2, 'data', $object, [$constraint2, $constraint3], 'group2');

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

Expand Down Expand Up @@ -172,8 +174,8 @@ public function testValidateConstraintsOptionEvenIfNoValidConstraint()
$parent->add($form);
$parent->submit([]);

$this->expectValidateValueAt(0, 'data', $object, $constraint1, 'group1');
$this->expectValidateValueAt(1, 'data', $object, $constraint2, 'group2');
$this->expectValidateValueAt(0, 'data', $object, [$constraint1], 'group1');
$this->expectValidateValueAt(1, 'data', $object, [$constraint2], 'group2');

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

Expand Down

0 comments on commit 2c1b1ba

Please sign in to comment.