2222use Symfony \Component \Form \FormConfigBuilder ;
2323use Symfony \Component \Form \FormError ;
2424use Symfony \Component \Form \FormInterface ;
25+ use Symfony \Component \Form \Tests \Extension \Validator \ViolationMapper \Fixtures \Issue ;
2526use Symfony \Component \PropertyAccess \PropertyPath ;
2627use Symfony \Component \Validator \ConstraintViolation ;
2728use Symfony \Component \Validator \ConstraintViolationInterface ;
@@ -70,12 +71,12 @@ protected function setUp(): void
7071 $ this ->params = ['foo ' => 'bar ' ];
7172 }
7273
73- protected function getForm ($ name = 'name ' , $ propertyPath = null , $ dataClass = null , $ errorMapping = [], $ inheritData = false , $ synchronized = true )
74+ protected function getForm ($ name = 'name ' , $ propertyPath = null , $ dataClass = null , $ errorMapping = [], $ inheritData = false , $ synchronized = true , array $ options = [] )
7475 {
7576 $ config = new FormConfigBuilder ($ name , $ dataClass , $ this ->dispatcher , [
7677 'error_mapping ' => $ errorMapping ,
77- ]);
78- $ config ->setMapped (true );
78+ ] + $ options );
79+ $ config ->setMapped (isset ( $ options [ ' mapped ' ]) ? $ options [ ' mapped ' ] : true );
7980 $ config ->setInheritData ($ inheritData );
8081 $ config ->setPropertyPath ($ propertyPath );
8182 $ config ->setCompound (true );
@@ -91,12 +92,9 @@ function () { throw new TransformationFailedException(); }
9192 return new Form ($ config );
9293 }
9394
94- /**
95- * @param $propertyPath
96- */
97- protected function getConstraintViolation ($ propertyPath ): ConstraintViolation
95+ protected function getConstraintViolation ($ propertyPath , $ root = null ): ConstraintViolation
9896 {
99- return new ConstraintViolation ($ this ->message , $ this ->messageTemplate , $ this ->params , null , $ propertyPath , null );
97+ return new ConstraintViolation ($ this ->message , $ this ->messageTemplate , $ this ->params , $ root , $ propertyPath , null );
10098 }
10199
102100 protected function getFormError (ConstraintViolationInterface $ violation , FormInterface $ form ): FormError
@@ -107,6 +105,33 @@ protected function getFormError(ConstraintViolationInterface $violation, FormInt
107105 return $ error ;
108106 }
109107
108+ public function testMappingErrorsWhenFormIsNotMapped ()
109+ {
110+ $ form = $ this ->getForm ('name ' , null , Issue::class, [
111+ 'child1 ' => 'child2 ' ,
112+ ]);
113+
114+ $ violation = $ this ->getConstraintViolation ('children[child1].data ' , $ form );
115+
116+ $ child1Form = $ this ->getForm ('child1 ' , null , null , [], false , true , [
117+ 'mapped ' => false ,
118+ ]);
119+ $ child2Form = $ this ->getForm ('child2 ' , null , null , [], false , true , [
120+ 'mapped ' => false ,
121+ ]);
122+
123+ $ form ->add ($ child1Form );
124+ $ form ->add ($ child2Form );
125+
126+ $ form ->submit ([]);
127+
128+ $ this ->mapper ->mapViolation ($ violation , $ form );
129+
130+ $ this ->assertCount (0 , $ form ->getErrors ());
131+ $ this ->assertCount (0 , $ form ->get ('child1 ' )->getErrors ());
132+ $ this ->assertCount (1 , $ form ->get ('child2 ' )->getErrors ());
133+ }
134+
110135 public function testMapToFormInheritingParentDataIfDataDoesNotMatch ()
111136 {
112137 $ violation = $ this ->getConstraintViolation ('children[address].data.foo ' );
0 commit comments