Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d418ebd

Browse filesBrowse files
committed
validate composite constraints in all groups
1 parent b8978bd commit d418ebd
Copy full SHA for d418ebd

File tree

Expand file treeCollapse file tree

2 files changed

+62
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+62
-1
lines changed

‎src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\FormInterface;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Constraints\Composite;
1617
use Symfony\Component\Validator\Constraints\GroupSequence;
1718
use Symfony\Component\Validator\Constraints\Valid;
1819
use Symfony\Component\Validator\ConstraintValidator;
@@ -90,7 +91,9 @@ public function validate($form, Constraint $formConstraint)
9091
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
9192

9293
// Prevent duplicate validation
93-
continue 2;
94+
if (!$constraint instanceof Composite) {
95+
continue 2;
96+
}
9497
}
9598
}
9699
}

‎src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Form\FormInterface;
2525
use Symfony\Component\Form\SubmitButtonBuilder;
2626
use Symfony\Component\Translation\IdentityTranslator;
27+
use Symfony\Component\Validator\Constraints\Collection;
2728
use Symfony\Component\Validator\Constraints\GroupSequence;
2829
use Symfony\Component\Validator\Constraints\NotBlank;
2930
use Symfony\Component\Validator\Constraints\NotNull;
@@ -673,6 +674,63 @@ public function testCauseForNotAllowedExtraFieldsIsTheFormConstraint()
673674
$this->assertSame($constraint, $context->getViolations()->get(0)->getConstraint());
674675
}
675676

677+
public function testNonCompositeConstraintValidatedOnce()
678+
{
679+
$form = $this
680+
->getBuilder('form', null, [
681+
'constraints' => [new NotBlank(['groups' => ['foo', 'bar']])],
682+
'validation_groups' => ['foo', 'bar'],
683+
])
684+
->setCompound(false)
685+
->getForm();
686+
$form->submit('');
687+
688+
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
689+
$this->validator->initialize($context);
690+
$this->validator->validate($form, new Form());
691+
692+
$this->assertCount(1, $context->getViolations());
693+
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
694+
$this->assertSame('data', $context->getViolations()[0]->getPropertyPath());
695+
}
696+
697+
public function testCompositeConstraintValidatedInEachGroup()
698+
{
699+
$form = $this->getBuilder('form', null, [
700+
'constraints' => [
701+
new Collection([
702+
'field1' => new NotBlank([
703+
'groups' => ['field1']
704+
]),
705+
'field2' => new NotBlank([
706+
'groups' => ['field2']
707+
]),
708+
]),
709+
],
710+
'validation_groups' => ['field1', 'field2'],
711+
])
712+
->setData([])
713+
->setCompound(true)
714+
->setDataMapper(new PropertyPathMapper())
715+
->getForm();
716+
$form->add($this->getForm('field1'));
717+
$form->add($this->getForm('field2'));
718+
$form->submit([
719+
'field1' => '',
720+
'field2' => '',
721+
]);
722+
723+
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
724+
$this->validator->initialize($context);
725+
$this->validator->validate($form, new Form());
726+
727+
$this->assertCount(2, $context->getViolations());
728+
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
729+
$this->assertSame('data[field1]', $context->getViolations()[0]->getPropertyPath());
730+
$this->assertSame('This value should not be blank.', $context->getViolations()[1]->getMessage());
731+
$this->assertSame('data[field2]', $context->getViolations()[1]->getPropertyPath());
732+
}
733+
676734
protected function createValidator()
677735
{
678736
return new FormValidator();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.