8000 [Form] Ignoring invalid forms from delete_empty behavior in CollectionType by yceruto · Pull Request #40258 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Ignoring invalid forms from delete_empty behavior in CollectionType #40258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public function onSubmit(FormEvent $event)
$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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function testOnSubmitDeleteEmptyNotCompoundEntriesIfAllowDelete()

$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);
Expand All @@ -282,11 +282,11 @@ public function testOnSubmitDeleteEmptyCompoundEntriesIfAllowDelete()

$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -211,6 +212,28 @@ public function testResizedDownIfSubmittedWithCompoundEmptyDataAndDeleteEmpty()
$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, [
Expand Down
0