[Form] group constraints when calling the validator
This commit is contained in:
parent
12f00e3e15
commit
d15f77f33e
@ -76,6 +76,8 @@ class FormValidator extends ConstraintValidator
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$groupedConstraints = [];
|
||||
|
||||
foreach ($constraints as $constraint) {
|
||||
// For the "Valid" constraint, validate the data in all groups
|
||||
if ($constraint instanceof Valid) {
|
||||
@ -88,7 +90,7 @@ class FormValidator extends ConstraintValidator
|
||||
// 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) {
|
||||
@ -97,6 +99,10 @@ class FormValidator extends ConstraintValidator
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($groupedConstraints as $group => $constraint) {
|
||||
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
|
||||
}
|
||||
}
|
||||
} elseif (!$form->isSynchronized()) {
|
||||
$childrenSynchronized = true;
|
||||
|
@ -26,6 +26,7 @@ use Symfony\Component\Form\SubmitButtonBuilder;
|
||||
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;
|
||||
@ -77,10 +78,11 @@ class FormValidatorTest extends ConstraintValidatorTestCase
|
||||
$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([]);
|
||||
@ -89,8 +91,8 @@ class FormValidatorTest extends ConstraintValidatorTestCase
|
||||
$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());
|
||||
|
||||
@ -172,8 +174,8 @@ class FormValidatorTest extends ConstraintValidatorTestCase
|
||||
$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());
|
||||
|
||||
|
Reference in New Issue
Block a user