|
16 | 16 | use Symfony\Component\EventDispatcher\EventDispatcher;
|
17 | 17 | use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
|
18 | 18 | use Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener;
|
| 19 | <
55F
span class="diff-text-marker">+use Symfony\Component\Form\Extension\Core\Type\CollectionType; |
| 20 | +use Symfony\Component\Form\Extension\Core\Type\FormType; |
19 | 21 | use Symfony\Component\Form\Extension\Core\Type\TextType;
|
20 | 22 | use Symfony\Component\Form\FormBuilder;
|
21 | 23 | use Symfony\Component\Form\FormEvent;
|
@@ -66,6 +68,21 @@ public function testPreSetDataResizesForm()
|
66 | 68 | $this->assertTrue($this->form->has('2'));
|
67 | 69 | }
|
68 | 70 |
|
| 71 | + public function testPreSetDataResizesFormWithIndexedName() |
| 72 | + { |
| 73 | + $this->form->add($this->getForm('my-id-0')); |
| 74 | + $this->form->add($this->getForm('my-id-1')); |
| 75 | + |
| 76 | + $data = ['my-id-2' => 'string', 'my-id-1' => 'string xy']; |
| 77 | + $event = new FormEvent($this->form, $data); |
| 78 | + $listener = new ResizeFormListener(TextType::class, ['attr' => ['maxlength' => 10]], false, false); |
| 79 | + $listener->preSetData($event); |
| 80 | + |
| 81 | + $this->assertFalse($this->form->has('my-id-0')); |
| 82 | + $this->assertTrue($this->form->has('my-id-1')); |
| 83 | + $this->assertTrue($this->form->has('my-id-2')); |
| 84 | + } |
| 85 | + |
69 | 86 | public function testPreSetDataRequiresArrayOrTraversable()
|
70 | 87 | {
|
71 | 88 | $this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException');
|
@@ -187,6 +204,18 @@ public function testOnSubmitNormDataRemovesEntriesMissingInTheFormIfAllowDelete(
|
187 | 204 | $this->assertEquals([1 => 'second'], $event->getData());
|
188 | 205 | }
|
189 | 206 |
|
| 207 | + public function testOnSubmitNormDataRemovesEntriesMissingInTheFormIfAllowDeleteWithIndexedName() |
| 208 | + { |
| 209 | + $this->form->add($this->getForm('my-id-1')); |
| 210 | + |
| 211 | + $data = ['my-id-0' => 'first', 'my-id-1' => 'second', 'my-id-2' => 'third']; |
| 212 | + $event = new FormEvent($this->form, $data); |
| 213 | + $listener = new ResizeFormListener('text', [], false, true); |
| 214 | + $listener->onSubmit($event); |
| 215 | + |
| 216 | + $this->assertEquals(['my-id-1' => 'second'], $event->getData()); |
| 217 | + } |
| 218 | + |
190 | 219 | public function testOnSubmitNormDataDoesNothingIfNotAllowDelete()
|
191 | 220 | {
|
192 | 221 | $this->form->add($this->getForm('1'));
|
@@ -292,4 +321,97 @@ public function testOnSubmitDeleteEmptyCompoundEntriesIfAllowDelete()
|
292 | 321 |
|
293 | 322 | $this->assertEquals(['0' => ['name' => 'John']], $event->getData());
|
294 | 323 | }
|
| 324 | + |
| 325 | + public function testOnSubmitDeleteEmptyCompoundEntriesIfAllowDeleteWithIndexedName() |
| 326 | + { |
| 327 | + $this->form->setData(['my-id-1' => ['name' => 'John'], 'my-id-2' => ['name' => 'Jane']]); |
| 328 | + $form1 = $this->getBuilder('my-id-1') |
| 329 | + ->setCompound(true) |
| 330 | + ->setDataMapper(new PropertyPathMapper()) |
| 331 | + ->getForm(); |
| 332 | + $form1->add($this->getForm('name')); |
| 333 | + $form2 = $this->getBuilder('my-id-2') |
| 334 | + ->setCompound(true) |
| 335 | + ->setDataMapper(new PropertyPathMapper()) |
| 336 | + ->getForm(); |
| 337 | + $form2->add($this->getForm('name')); |
| 338 | + $this->form->add($form1); |
| 339 | + $this->form->add($form2); |
| 340 | + |
| 341 | + $data = ['my-id-1' => ['name' => 'John'], 'my-id-2' => ['name' => '']]; |
| 342 | + foreach ($data as $child => $dat) { |
| 343 | + $this->form->get($child)->setData($dat); |
| 344 | + } |
| 345 | + $event = new FormEvent($this->form, $data); |
| 346 | + $callback = function ($data) { |
| 347 | + return '' === $data['name']; |
| 348 | + }; |
| 349 | + $listener = new ResizeFormListener('text', [], false, true, $callback); |
| 350 | + $listener->onSubmit($event); |
| 351 | + |
| 352 | + $this->assertEquals(['my-id-1' => ['name' => 'John']], $event->getData()); |
| 353 | + } |
| 354 | + |
| 355 | + public function testIndexedNameFeature() |
| 356 | + { |
| 357 | + $form = $this->factory->createNamedBuilder('root', FormType::class, ['items' => null]) |
| 358 | + ->add('items', CollectionType::class, [ |
| 359 | + 'entry_type' => TextType::class, |
| 360 | + 'allow_add' => true, |
| 361 | + 'data' => ['foo'], |
| 362 | + 'index_name' => 'id', |
| 363 | + ]) |
| 364 | + ->getForm() |
| 365 | + ; |
| 366 | + |
| 367 | + $this->assertSame(['foo'], $form->get('items')->getData()); |
| 368 | + $form->submit(['items' => ['foo', 'my-id-1' => 'foo', 'my-id-2' => 'bar']]); |
| 369 | + $this->assertSame(['foo', 'my-id-1' => 'foo', 'my-id-2' => 'bar'], $form->get('items')->getData()); |
| 370 | + } |
| 371 | + |
| 372 | + public function testIndexedNameFeatureWithAllowDelete() |
| 373 | + { |
| 374 | + $form = $this->factory->createNamedBuilder('root', FormType::class, ['items' => null]) |
| 375 | + ->add('items', CollectionType::class, [ |
| 376 | + 'entry_type' => TextType::class, |
| 377 | + 'allow_add' => true, |
| 378 | + 'allow_delete' => true, |
| 379 | + 'data' => ['foo'], |
| 380 | + 'index_name' => 'id', |
| 381 | + ]) |
| 382 | + ->getForm() |
| 383 | + ; |
| 384 | + |
| 385 | + $this->assertSame(['foo'], $form->get('items')->getData()); |
| 386 | + $form->submit(['items' => ['my-id-1' => 'foo', 'my-id-2' => 'bar']]); |
| 387 | + $this->assertSame(['my-id-1' => 'foo', 'my-id-2' => 'bar'], $form->get('items')->getData()); |
| 388 | + } |
| 389 | + |
| 390 | + public function testIndexedNameFeatureWithSimulatedArray() |
| 391 | + { |
| 392 | + $form = $this->factory->createNamedBuilder('root', FormType::class, ['items' => null]) |
| 393 | + ->add('items', CollectionType::class, [ |
| 394 | + 'entry_type' => FooType::class, |
| 395 | + 'allow_add' => true, |
| 396 | + 'allow_delete' => true, |
| 397 | + 'data' => $data = [ |
| 398 | + ['id' => 'custom-id-1', 'foo' => 'bar'], |
| 399 | + ['id' => 'custom-id-2', 'foo' => 'me'], |
| 400
9B02
| + ['id' => 'custom-id-3', 'foo' => 'foo'], |
| 401 | + ], |
| 402 | + 'index_name' => 'id', |
| 403 | + ]) |
| 404 | + ->getForm() |
| 405 | + ; |
| 406 | + |
| 407 | + $this->assertSame($data, $form->get('items')->getData()); |
| 408 | + $form->submit(['items' => [ |
| 409 | + 'custom-id-3' => ['foo' => 'foo 2'], |
| 410 | + 'custom-id-1' => ['foo' => 'bar 2'], |
| 411 | + ]]); |
| 412 | + $this->assertSame([ |
| 413 | + 'custom-id-1' => ['id' => 'custom-id-1', 'foo' => 'bar 2'], |
| 414 | + 'custom-id-3' => ['id' => 'custom-id-3', 'foo' => 'foo 2'], |
| 415 | + ], $form->get('items')->getData()); |
| 416 | + } |
295 | 417 | }
|
0 commit comments