|
18 | 18 | use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
|
19 | 19 | use Symfony\Component\Form\Extension\Validator\Constraints\Form;
|
20 | 20 | use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator;
|
| 21 | +use Symfony\Component\Form\Extension\Validator\ValidatorExtension; |
21 | 22 | use Symfony\Component\Form\FormBuilder;
|
22 | 23 | use Symfony\Component\Form\FormFactoryBuilder;
|
23 | 24 | use Symfony\Component\Form\FormFactoryInterface;
|
@@ -51,7 +52,9 @@ class FormValidatorTest extends ConstraintValidatorTestCase
|
51 | 52 | protected function setUp()
|
52 | 53 | {
|
53 | 54 | $this->dispatcher = new EventDispatcher();
|
54 |
| - $this->factory = (new FormFactoryBuilder())->getFormFactory(); |
| 55 | + $this->factory = (new FormFactoryBuilder()) |
| 56 | + ->addExtension(new ValidatorExtension(Validation::createValidator())) |
| 57 | + ->getFormFactory(); |
55 | 58 |
|
56 | 59 | parent::setUp();
|
57 | 60 |
|
@@ -791,6 +794,61 @@ public function testCompositeConstraintValidatedInSequence()
|
791 | 794 | $this->assertSame('data[field1]', $context->getViolations()[0]->getPropertyPath());
|
792 | 795 | }
|
793 | 796 |
|
| 797 | + public function testCascadeValidationToChildFormsUsingPropertyPaths() |
| 798 | + { |
| 799 | + $form = $this->getCompoundForm([], [ |
| 800 | + 'validation_groups' => ['group1', 'group2'], |
| 801 | + ]) |
| 802 | + ->add('field1', null, [ |
| 803 | + 'constraints' => [new NotBlank(['groups' => 'group1'])], |
| 804 | + 'property_path' => '[foo]', |
| 805 | + ]) |
| 806 | + ->add('field2', null, [ |
| 807 | + 'constraints' => [new NotBlank(['groups' => 'group2'])], |
| 808 | + 'property_path' => '[bar]', |
| 809 | + ]) |
| 810 | + ; |
| 811 | + |
| 812 | + $form->submit([ |
| 813 | + 'field1' => '', |
| 814 | + 'field2' => '', |
| 815 | + ]); |
| 816 | + |
| 817 | + $context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator()); |
| 818 | + $this->validator->initialize($context); |
| 819 | + $this->validator->validate($form, new Form()); |
| 820 | + |
| 821 | + $this->assertCount(2, $context->getViolations()); |
| 822 | + $this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage()); |
| 823 | + $this->assertSame('children[field1].data', $context->getViolations()[0]->getPropertyPath()); |
| 824 | + $this->assertSame('This value should not be blank.', $context->getViolations()[1]->getMessage()); |
| 825 | + $this->assertSame('children[field2].data', $context->getViolations()[1]->getPropertyPath()); |
| 826 | + } |
| 827 | + |
| 828 | + public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence() |
| 829 | + { |
| 830 | + $form = $this->getCompoundForm([], [ |
| 831 | + 'validation_groups' => new GroupSequence(['group1', 'group2']), |
| 832 | + ]) |
| 833 | + ->add('field1', null, [ |
| 834 | + 'constraints' => [new NotBlank(['groups' => 'group1'])], |
| 835 | + 'property_path' => '[foo]', |
| 836 | + ]) |
| 837 | + ; |
| 838 | + |
| 839 | + $form->submit([ |
| 840 | + 'field1' => '', |
| 841 | + ]); |
| 842 | + |
| 843 | + $context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator()); |
| 844 | + $this->validator->initialize($context); |
| 845 | + $this->validator->validate($form, new Form()); |
| 846 | + |
| 847 | + $this->assertCount(1, $context->getViolations()); |
| 848 | + $this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage()); |
| 849 | + $this->assertSame('children[field1].data', $context->getViolations()[0]->getPropertyPath()); |
| 850 | + } |
| 851 | + |
794 | 852 | protected function createValidator()
|
795 | 853 | {
|
796 | 854 | return new FormValidator();
|
|
0 commit comments