|
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; |
@@ -52,7 +53,9 @@ class FormValidatorTest extends ConstraintValidatorTestCase |
52 | 53 | protected function setUp(): void |
53 | 54 | { |
54 | 55 | $this->dispatcher = new EventDispatcher(); |
55 | | - $this->factory = (new FormFactoryBuilder())->getFormFactory(); |
| 56 | + $this->factory = (new FormFactoryBuilder()) |
| 57 | + ->addExtension(new ValidatorExtension(Validation::createValidator())) |
| 58 | + ->getFormFactory(); |
56 | 59 |
|
57 | 60 | parent::setUp(); |
58 | 61 |
|
@@ -834,6 +837,61 @@ public function testCompositeConstraintValidatedInSequence() |
834 | 837 | $this->assertSame('data[field1]', $context->getViolations()[0]->getPropertyPath()); |
835 | 838 | } |
836 | 839 |
|
| 840 | + public function testCascadeValidationToChildFormsUsingPropertyPaths() |
| 841 | + { |
| 842 | + $form = $this->getCompoundForm([], [ |
| 843 | + 'validation_groups' => ['group1', 'group2'], |
| 844 | + ]) |
| 845 | + ->add('field1', null, [ |
| 846 | + 'constraints' => [new NotBlank(['groups' => 'group1'])], |
| 847 | + 'property_path' => '[foo]', |
| 848 | + ]) |
| 849 | + ->add('field2', null, [ |
| 850 | + 'constraints' => [new NotBlank(['groups' => 'group2'])], |
| 851 | + 'property_path' => '[bar]', |
| 852 | + ]) |
| 853 | + ; |
| 854 | + |
| 855 | + $form->submit([ |
| 856 | + 'field1' => '', |
| 857 | + 'field2' => '', |
| 858 | + ]); |
| 859 | + |
| 860 | + $context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator()); |
| 861 | + $this->validator->initialize($context); |
| 862 | + $this->validator->validate($form, new Form()); |
| 863 | + |
| 864 | + $this->assertCount(2, $context->getViolations()); |
| 865 | + $this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage()); |
| 866 | + $this->assertSame('children[field1].data', $context->getViolations()[0]->getPropertyPath()); |
| 867 | + $this->assertSame('This value should not be blank.', $context->getViolations()[1]->getMessage()); |
| 868 | + $this->assertSame('children[field2].data', $context->getViolations()[1]->getPropertyPath()); |
| 869 | + } |
| 870 | + |
| 871 | + public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence() |
| 872 | + { |
| 873 | + $form = $this->getCompoundForm([], [ |
| 874 | + 'validation_groups' => new GroupSequence(['group1', 'group2']), |
| 875 | + ]) |
| 876 | + ->add('field1', null, [ |
| 877 | + 'constraints' => [new NotBlank(['groups' => 'group1'])], |
| 878 | + 'property_path' => '[foo]', |
| 879 | + ]) |
| 880 | + ; |
| 881 | + |
| 882 | + $form->submit([ |
| 883 | + 'field1' => '', |
| 884 | + ]); |
| 885 | + |
| 886 | + $context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator()); |
| 887 | + $this->validator->initialize($context); |
| 888 | + $this->validator->validate($form, new Form()); |
| 889 | + |
| 890 | + $this->assertCount(1, $context->getViolations()); |
| 891 | + $this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage()); |
| 892 | + $this->assertSame('children[field1].data', $context->getViolations()[0]->getPropertyPath()); |
| 893 | + } |
| 894 | + |
837 | 895 | protected function createValidator() |
838 | 896 | { |
839 | 897 | return new FormValidator(); |
|
0 commit comments