Skip to content

Commit d2008c8

Browse files
ycerutofabpot
authored andcommitted
[Form] Ignoring invalid forms from delete_empty behavior in CollectionType
1 parent ad46245 commit d2008c8

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

Extension/Core/EventListener/ResizeFormListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function onSubmit(FormEvent $event)
132132
$previousData = $form->getData();
133133
/** @var FormInterface $child */
134134
foreach ($form as $name => $child) {
135+
if (!$child->isValid() || !$child->isSynchronized()) {
136+
continue;
137+
}
138+
135139
$isNew = !isset($previousData[$name]);
136140
$isEmpty = \is_callable($this->deleteEmpty) ? ($this->deleteEmpty)($child->getData()) : $child->isEmpty();
137141

Tests/Extension/Core/EventListener/ResizeFormListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public function testOnSubmitDeleteEmptyNotCompoundEntriesIfAllowDelete()
255255

256256
$data = [0 => 'first', 1 => ''];
257257
foreach ($data as $child => $dat) {
258-
$this->form->get($child)->setData($dat);
258+
$this->form->get($child)->submit($dat);
259259
}
260260
$event = new FormEvent($this->form, $data);
261261
$listener = new ResizeFormListener('text', [], false, true, true);
@@ -282,11 +282,11 @@ public function testOnSubmitDeleteEmptyCompoundEntriesIfAllowDelete()
282282

283283
$data = ['0' => ['name' => 'John'], '1' => ['name' => '']];
284284
foreach ($data as $child => $dat) {
285-
$this->form->get($child)->setData($dat);
285+
$this->form->get($child)->submit($dat);
286286
}
287287
$event = new FormEvent($this->form, $data);
288288
$callback = function ($data) {
289-
return '' === $data['name'];
289+
return null === $data['name'];
290290
};
291291
$listener = new ResizeFormListener('text', [], false, true, $callback);
292292
$listener->onSubmit($event);

Tests/Extension/Core/Type/CollectionTypeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\Exception\UnexpectedTypeException;
15+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
1516
use Symfony\Component\Form\Form;
1617
use Symfony\Component\Form\Tests\Fixtures\Author;
1718
use Symfony\Component\Form\Tests\Fixtures\AuthorType;
@@ -211,6 +212,28 @@ public function testResizedDownIfSubmittedWithCompoundEmptyDataAndDeleteEmpty()
211212
$this->assertEquals([new Author('s_first', 's_last')], $form->getData());
212213
}
213214

215+
public function testNotDeleteEmptyIfInvalid()
216+
{
217+
$form = $this->factory->create(static::TESTED_TYPE, null, [
218+
'entry_type' => ChoiceType::class,
219+
'entry_options' => [
220+
'choices' => ['a', 'b'],
221+
],
222+
'allow_add' => true,
223+
'allow_delete' => true,
224+
'delete_empty' => true,
225+
]);
226+
227+
$form->submit(['a', 'x', '']);
228+
229+
$this->assertSame(['a'], $form->getData());
230+
$this->assertCount(2, $form);
231+
$this->assertTrue($form->has('1'));
232+
$this->assertFalse($form[1]->isValid());
233+
$this->assertNull($form[1]->getData());
234+
$this->assertSame('x', $form[1]->getViewData());
235+
}
236+
214237
public function testNotResizedIfSubmittedWithExtraData()
215238
{
216239
$form = $this->factory->create(static::TESTED_TYPE, null, [

0 commit comments

Comments
 (0)