|
24 | 24 | use Symfony\Component\Form\FormInterface;
|
25 | 25 | use Symfony\Component\Form\SubmitButtonBuilder;
|
26 | 26 | use Symfony\Component\Translation\IdentityTranslator;
|
| 27 | +use Symfony\Component\Validator\Constraints\Collection; |
27 | 28 | use Symfony\Component\Validator\Constraints\GroupSequence;
|
28 | 29 | use Symfony\Component\Validator\Constraints\NotBlank;
|
29 | 30 | use Symfony\Component\Validator\Constraints\NotNull;
|
@@ -673,6 +674,63 @@ public function testCauseForNotAllowedExtraFieldsIsTheFormConstraint()
|
673 | 674 | $this->assertSame($constraint, $context->getViolations()->get(0)->getConstraint());
|
674 | 675 | }
|
675 | 676 |
|
| 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 | + |
676 | 734 | protected function createValidator()
|
677 | 735 | {
|
678 | 736 | return new FormValidator();
|
|
0 commit comments