bug #40258 [Form] Ignoring invalid forms from delete_empty behavior in CollectionType (yceruto)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Form] Ignoring invalid forms from delete_empty behavior in CollectionType

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37660
| License       | MIT
| Doc PR        | -

As expained on linked issue, empty forms cannot be removed if they are invalid upon submitting (e.g. transformation failures). Thus, form errors can be displayed properly to the end user.

Commits
-------

e4911554e3 [Form] Ignoring invalid forms from delete_empty behavior in CollectionType
This commit is contained in:
Fabien Potencier 2021-02-21 18:50:09 +01:00
commit 8054d1d04c
3 changed files with 30 additions and 3 deletions

View File

@ -132,6 +132,10 @@ class ResizeFormListener implements EventSubscriberInterface
$previousData = $form->getData();
/** @var FormInterface $child */
foreach ($form as $name => $child) {
if (!$child->isValid() || !$child->isSynchronized()) {
continue;
}
$isNew = !isset($previousData[$name]);
$isEmpty = \is_callable($this->deleteEmpty) ? ($this->deleteEmpty)($child->getData()) : $child->isEmpty();

View File

@ -255,7 +255,7 @@ class ResizeFormListenerTest extends TestCase
$data = [0 => 'first', 1 => ''];
foreach ($data as $child => $dat) {
$this->form->get($child)->setData($dat);
$this->form->get($child)->submit($dat);
}
$event = new FormEvent($this->form, $data);
$listener = new ResizeFormListener('text', [], false, true, true);
@ -282,11 +282,11 @@ class ResizeFormListenerTest extends TestCase
$data = ['0' => ['name' => 'John'], '1' => ['name' => '']];
foreach ($data as $child => $dat) {
$this->form->get($child)->setData($dat);
$this->form->get($child)->submit($dat);
}
$event = new FormEvent($this->form, $data);
$callback = function ($data) {
return '' === $data['name'];
return null === $data['name'];
};
$listener = new ResizeFormListener('text', [], false, true, $callback);
$listener->onSubmit($event);

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\Tests\Fixtures\Author;
use Symfony\Component\Form\Tests\Fixtures\AuthorType;
@ -211,6 +212,28 @@ class CollectionTypeTest extends BaseTypeTest
$this->assertEquals([new Author('s_first', 's_last')], $form->getData());
}
public function testNotDeleteEmptyIfInvalid()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'entry_type' => ChoiceType::class,
'entry_options' => [
'choices' => ['a', 'b'],
],
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
]);
$form->submit(['a', 'x', '']);
$this->assertSame(['a'], $form->getData());
$this->assertCount(2, $form);
$this->assertTrue($form->has('1'));
$this->assertFalse($form[1]->isValid());
$this->assertNull($form[1]->getData());
$this->assertSame('x', $form[1]->getViewData());
}
public function testNotResizedIfSubmittedWithExtraData()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [