|
12 | 12 | namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Form\Tests\Fixtures\Author;
|
| 15 | +use Symfony\Component\Form\Tests\Fixtures\AuthorType; |
15 | 16 |
|
16 | 17 | class CollectionTypeTest extends BaseTypeTest
|
17 | 18 | {
|
@@ -110,6 +111,49 @@ public function testResizedDownIfSubmittedWithEmptyDataAndDeleteEmpty()
|
110 | 111 | $this->assertEquals(array('foo@foo.com'), $form->getData());
|
111 | 112 | }
|
112 | 113 |
|
| 114 | + public function testResizedDownWithDeleteEmptyCallable() |
| 115 | + { |
| 116 | + $form = $this->factory->create(static::TESTED_TYPE, null, array( |
| 117 | + 'entry_type' => AuthorType::class, |
| 118 | + 'allow_delete' => true, |
| 119 | + 'delete_empty' => function (Author $obj = null) { |
| 120 | + return null === $obj || empty($obj->firstName); |
| 121 | + }, |
| 122 | + )); |
| 123 | + |
| 124 | + $form->setData(array(new Author('Bob'), new Author('Alice'))); |
| 125 | + $form->submit(array(array('firstName' => 'Bob'), array('firstName' => ''))); |
| 126 | + |
| 127 | + $this->assertTrue($form->has('0')); |
| 128 | + $this->assertFalse($form->has('1')); |
| 129 | + $this->assertEquals(new Author('Bob'), $form[0]->getData()); |
| 130 | + $this->assertEquals(array(new Author('Bob')), $form->getData()); |
| 131 | + } |
| 132 | + |
| 133 | + public function testResizedDownIfSubmittedWithCompoundEmptyDataDeleteEmptyAndNoDataClass() |
| 134 | + { |
| 135 | + $form = $this->factory->create(static::TESTED_TYPE, null, array( |
| 136 | + 'entry_type' => AuthorType::class, |
| 137 | + // If the field is not required, no new Author will be created if the |
| 138 | + // form is completely empty |
| 139 | + 'entry_options' => array('data_class' => null), |
| 140 | + 'allow_add' => true, |
| 141 | + 'allow_delete' => true, |
| 142 | + 'delete_empty' => function ($author) { |
| 143 | + return empty($author['firstName']); |
| 144 | + }, |
| 145 | + )); |
| 146 | + $form->setData(array(array('firstName' => 'first', 'lastName' => 'last'))); |
| 147 | + $form->submit(array( |
| 148 | + array('firstName' => 's_first', 'lastName' => 's_last'), |
| 149 | + array('firstName' => '', 'lastName' => ''), |
| 150 | + )); |
| 151 | + $this->assertTrue($form->has('0')); |
| 152 | + $this->assertFalse($form->has('1')); |
| 153 | + $this->assertEquals(array('firstName' => 's_first', 'lastName' => 's_last'), $form[0]->getData()); |
| 154 | + $this->assertEquals(array(array('firstName' => 's_first', 'lastName' => 's_last')), $form->getData()); |
| 155 | + } |
| 156 | + |
113 | 157 | public function testDontAddEmptyDataIfDeleteEmpty()
|
114 | 158 | {
|
115 | 159 | $form = $this->factory->create(static::TESTED_TYPE, null, array(
|
|
0 commit comments