Skip to content

Commit

Permalink
validate composite constraints in all groups
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jun 14, 2019
1 parent b8978bd commit d418ebd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
Expand Up @@ -13,6 +13,7 @@

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;
Expand Down Expand Up @@ -90,7 +91,9 @@ public function validate($form, Constraint $formConstraint)
$validator->atPath('data')->validate($form->getData(), $constraint, $group);

// Prevent duplicate validation
continue 2;
if (!$constraint instanceof Composite) {
continue 2;
}
}
}
}
Expand Down
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -673,6 +674,63 @@ public function testCauseForNotAllowedExtraFieldsIsTheFormConstraint()
$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();
Expand Down

0 comments on commit d418ebd

Please sign in to comment.