Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] group constraints when calling the validator #34464

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- constraint
+ constraints

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would override the $constraints variable used above.

Copy link
Contributor

@dmaicher dmaicher Nov 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name it $constraintsInGroup or so? Slightly confusing otherwise 🙈

$validator->atPath('data')->validate($form->getData(), $constraint, $group);
}
}
} elseif (!$form->isSynchronized()) {
$childrenSynchronized = true;
Expand Down
Original file line number Diff line number Diff line change
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