19
19
use Symfony \Component \Form \Tests \Extension \Core \Type \FormTypeTest ;
20
20
use Symfony \Component \Form \Tests \Extension \Core \Type \TextTypeTest ;
21
21
use Symfony \Component \Form \Tests \Fixtures \Author ;
22
- use Symfony \Component \OptionsResolver \Exception \InvalidOptionsException ;
23
22
use Symfony \Component \Form \Tests \Fixtures \AuthorType ;
24
23
use Symfony \Component \Form \Tests \Fixtures \Organization ;
24
+ use Symfony \Component \OptionsResolver \Exception \InvalidOptionsException ;
25
25
use Symfony \Component \Validator \Constraints \GroupSequence ;
26
26
use Symfony \Component \Validator \Constraints \Length ;
27
27
use Symfony \Component \Validator \Constraints \NotBlank ;
@@ -192,21 +192,7 @@ public function testErrorPathOnCollections()
192
192
->setMetadataFactory ($ metadataFactory )
193
193
->getValidator ();
194
194
195
- $ form = Forms::createFormFactoryBuilder ()
196
- ->addExtension (new ValidatorExtension ($ validator ))
197
- ->getFormFactory ()
198
- ->create (FormTypeTest::TESTED_TYPE , new Organization ([]), [
199
- 'data_class ' => Organization::class,
200
- 'by_reference ' => false ,
201
- ])
202
- ->add ('authors ' , CollectionTypeTest::TESTED_TYPE , [
203
- 'entry_type ' => AuthorType::class,
204
- 'allow_add ' => true ,
205
- 'allow_delete ' => true ,
206
- ])
207
- ;
208
-
209
- $ form ->submit ([
195
+ $ submitData = [
210
196
'authors ' => [
211
197
0 => [
212
198
'firstName ' => '' , // Fires a Not Blank Error
@@ -222,7 +208,23 @@ public function testErrorPathOnCollections()
222
208
'lastName ' => 'lastName3 ' ,
223
209
],
224
210
],
225
- ]);
211
+ ];
212
+
213
+ $ form = Forms::createFormFactoryBuilder ()
214
+ ->addExtension (new ValidatorExtension ($ validator ))
215
+ ->getFormFactory ()
216
+ ->create (FormTypeTest::TESTED_TYPE , new Organization ([]), [
217
+ 'data_class ' => Organization::class,
218
+ 'by_reference ' => false ,
219
+ ])
220
+ ->add ('authors ' , CollectionTypeTest::TESTED_TYPE , [
221
+ 'entry_type ' => AuthorType::class,
222
+ 'allow_add ' => true ,
223
+ 'allow_delete ' => true ,
224
+ ])
225
+ ;
226
+
227
+ $ form ->submit ($ submitData );
226
228
227
229
//Form behaves right (...?). It has index 0, 2 and 3 (1 has been removed)
228
230
$ this ->assertTrue ($ form ->get ('authors ' )->has ('0 ' ));
@@ -242,11 +244,53 @@ public function testErrorPathOnCollections()
242
244
];
243
245
244
246
$ this ->assertContains ('data.authors[0].firstName ' , $ errorPaths );
245
- $ this ->assertNotContains ('data.authors[1].firstName ' , $ errorPaths );
247
+ $ this ->assertContains ('data.authors[1].firstName ' , $ errorPaths );
246
248
$ this ->assertContains ('data.authors[2].firstName ' , $ errorPaths );
247
- $ this ->assertContains ('data.authors[3].firstName ' , $ errorPaths );
249
+ $ this ->assertNotContains ('data.authors[3].firstName ' , $ errorPaths );
248
250
249
251
//In fact, root form should NOT contain errors but it does
252
+ $ this ->assertCount (1 , $ form ->getErrors (false ));
253
+
254
+ //Let's do this again, but with "keep_as_list" option set to true
255
+ $ form = Forms::createFormFactoryBuilder ()
256
+ ->addExtension (new ValidatorExtension ($ validator ))
257
+ ->getFormFactory ()
258
+ ->create (FormTypeTest::TESTED_TYPE , new Organization ([]), [
259
+ 'data_class ' => Organization::class,
260
+ 'by_reference ' => false ,
261
+ ])
262
+ ->add ('authors ' , CollectionTypeTest::TESTED_TYPE , [
263
+ 'entry_type ' => AuthorType::class,
264
+ 'allow_add ' => true ,
265
+ 'allow_delete ' => true ,
266
+ 'keep_as_list ' => true ,
267
+ ])
268
+ ;
269
+
270
+ $ form ->submit ($ submitData );
271
+
272
+ //Errors paths are not messing up now
273
+ $ this ->assertTrue ($ form ->get ('authors ' )->has ('0 ' ));
274
+ $ this ->assertTrue ($ form ->get ('authors ' )->has ('1 ' ));
275
+ $ this ->assertTrue ($ form ->get ('authors ' )->has ('2 ' ));
276
+ $ this ->assertNotTrue ($ form ->get ('authors ' )->has ('3 ' ));
277
+
278
+ //Form does have 3 not blank errors
279
+ $ errors = $ form ->getErrors (true );
280
+ $ this ->assertCount (3 , $ errors );
281
+
282
+ $ errorPaths = [
283
+ $ errors [0 ]->getCause ()->getPropertyPath (),
284
+ $ errors [1 ]->getCause ()->getPropertyPath (),
285
+ $ errors [2 ]->getCause ()->getPropertyPath (),
286
+ ];
287
+
288
+ $ this ->assertContains ('data.authors[0].firstName ' , $ errorPaths );
289
+ $ this ->assertContains ('data.authors[1].firstName ' , $ errorPaths );
290
+ $ this ->assertContains ('data.authors[2].firstName ' , $ errorPaths );
291
+ $ this ->assertNotContains ('data.authors[3].firstName ' , $ errorPaths );
292
+
293
+ //Root form does NOT contain errors
250
294
$ this ->assertCount (0 , $ form ->getErrors (false ));
251
295
}
252
296
}
0 commit comments