|
18 | 18 | useSymfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; |
19 | 19 | useSymfony\Component\Form\Extension\Validator\Constraints\Form; |
20 | 20 | useSymfony\Component\Form\Extension\Validator\Constraints\FormValidator; |
| 21 | +useSymfony\Component\Form\Extension\Validator\ValidatorExtension; |
21 | 22 | useSymfony\Component\Form\FormBuilder; |
22 | 23 | useSymfony\Component\Form\FormFactoryBuilder; |
23 | 24 | useSymfony\Component\Form\FormFactoryInterface; |
@@ -51,7 +52,9 @@ class FormValidatorTest extends ConstraintValidatorTestCase |
51 | 52 | protectedfunctionsetUp() |
52 | 53 | { |
53 | 54 | $this->dispatcher =newEventDispatcher(); |
54 | | -$this->factory = (newFormFactoryBuilder())->getFormFactory(); |
| 55 | +$this->factory = (newFormFactoryBuilder()) |
| 56 | + ->addExtension(newValidatorExtension(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 | +publicfunctiontestCascadeValidationToChildFormsUsingPropertyPaths() |
| 798 | + { |
| 799 | +$form =$this->getCompoundForm([], [ |
| 800 | +'validation_groups' => ['group1','group2'], |
| 801 | + ]) |
| 802 | + ->add('field1',null, [ |
| 803 | +'constraints' => [newNotBlank(['groups' =>'group1'])], |
| 804 | +'property_path' =>'[foo]', |
| 805 | + ]) |
| 806 | + ->add('field2',null, [ |
| 807 | +'constraints' => [newNotBlank(['groups' =>'group2'])], |
| 808 | +'property_path' =>'[bar]', |
| 809 | + ]) |
| 810 | + ; |
| 811 | + |
| 812 | +$form->submit([ |
| 813 | +'field1' =>'', |
| 814 | +'field2' =>'', |
| 815 | + ]); |
| 816 | + |
| 817 | +$context =newExecutionContext(Validation::createValidator(),$form,newIdentityTranslator()); |
| 818 | +$this->validator->initialize($context); |
| 819 | +$this->validator->validate($form,newForm()); |
| 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 | +publicfunctiontestCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence() |
| 829 | + { |
| 830 | +$form =$this->getCompoundForm([], [ |
| 831 | +'validation_groups' =>newGroupSequence(['group1','group2']), |
| 832 | + ]) |
| 833 | + ->add('field1',null, [ |
| 834 | +'constraints' => [newNotBlank(['groups' =>'group1'])], |
| 835 | +'property_path' =>'[foo]', |
| 836 | + ]) |
| 837 | + ; |
| 838 | + |
| 839 | +$form->submit([ |
| 840 | +'field1' =>'', |
| 841 | + ]); |
| 842 | + |
| 843 | +$context =newExecutionContext(Validation::createValidator(),$form,newIdentityTranslator()); |
| 844 | +$this->validator->initialize($context); |
| 845 | +$this->validator->validate($form,newForm()); |
| 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 | protectedfunctioncreateValidator() |
795 | 853 | { |
796 | 854 | returnnewFormValidator(); |
|