validate composite constraints in all groups

This commit is contained in:
Christian Flothmann 2019-06-14 09:11:59 +02:00
parent b8978bd9ff
commit 94ded00216
2 changed files with 62 additions and 1 deletions

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Form\Extension\Validator\Constraints;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Composite;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidator;
@ -90,7 +91,9 @@ class FormValidator extends ConstraintValidator
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
// Prevent duplicate validation
continue 2;
if (!$constraint instanceof Composite) {
continue 2;
}
}
}
}

View File

@ -24,6 +24,7 @@ use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
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\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
@ -673,6 +674,63 @@ class FormValidatorTest extends ConstraintValidatorTestCase
$this->assertSame($constraint, $context->getViolations()->get(0)->getConstraint());
}
public function testNonCompositeConstraintValidatedOnce()
{
$form = $this
->getBuilder('form', null, [
'constraints' => [new NotBlank(['groups' => ['foo', 'bar']])],
'validation_groups' => ['foo', 'bar'],
])
->setCompound(false)
->getForm();
$form->submit('');
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
$this->validator->initialize($context);
$this->validator->validate($form, new Form());
$this->assertCount(1, $context->getViolations());
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
$this->assertSame('data', $context->getViolations()[0]->getPropertyPath());
}
public function testCompositeConstraintValidatedInEachGroup()
{
$form = $this->getBuilder('form', null, [
'constraints' => [
new Collection([
'field1' => new NotBlank([
'groups' => ['field1'],
]),
'field2' => new NotBlank([
'groups' => ['field2'],
]),
]),
],
'validation_groups' => ['field1', 'field2'],
])
->setData([])
->setCompound(true)
->setDataMapper(new PropertyPathMapper())
->getForm();
$form->add($this->getForm('field1'));
$form->add($this->getForm('field2'));
$form->submit([
'field1' => '',
'field2' => '',
]);
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
$this->validator->initialize($context);
$this->validator->validate($form, new Form());
$this->assertCount(2, $context->getViolations());
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
$this->assertSame('data[field1]', $context->getViolations()[0]->getPropertyPath());
$this->assertSame('This value should not be blank.', $context->getViolations()[1]->getMessage());
$this->assertSame('data[field2]', $context->getViolations()[1]->getPropertyPath());
}
protected function createValidator()
{
return new FormValidator();