Skip to content

[Form] Allow pass filter callback to delete_empty option. #20496

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
Jul 26, 2017
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 @@ -48,7 +48,7 @@ class ResizeFormListener implements EventSubscriberInterface
protected $allowDelete;

/**
* @var bool
* @var bool|callable
*/
private $deleteEmpty;

Expand Down Expand Up @@ -148,14 +148,15 @@ public function onSubmit(FormEvent $event)
throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)');
}

if ($this->deleteEmpty) {
$previousData = $event->getForm()->getData();
if ($entryFilter = $this->deleteEmpty) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess $entryFilter can be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? This variable uses some lines bottom

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, but why not use $this->deleteEmpty directly? IMHO $this->deleteEmpty($child->getData()) is quite easy to understand too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because $this->deleteEmpty() will call undefined method ResizeFormListener::deleteEmpty()

Copy link
Contributor

@ogizanagi ogizanagi Jul 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and ($this->deleteEmpty)($child->getData()) is only doable in PHP 7.0+

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, my bad ;)

$previousData = $form->getData();
foreach ($form as $name => $child) {
$isNew = !isset($previousData[$name]);
$isEmpty = is_callable($entryFilter) ? $entryFilter($child->getData()) : $child->isEmpty();

// $isNew can only be true if allowAdd is true, so we don't
// need to check allowAdd again
if ($child->isEmpty() && ($isNew || $this->allowDelete)) {
if ($isEmpty && ($isNew || $this->allowDelete)) {
unset($data[$name]);
$form->remove($name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function configureOptions(OptionsResolver $resolver)
));

$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
$resolver->setAllowedTypes('delete_empty', array('bool', 'callable'));
}

/**
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\Tests\Fixtures\Author;
use Symfony\Component\Form\Tests\Fixtures\AuthorType;

class CollectionTypeTest extends BaseTypeTest
{
Expand Down Expand Up @@ -110,6 +111,49 @@ public function testResizedDownIfSubmittedWithEmptyDataAndDeleteEmpty()
$this->assertEquals(array('foo@foo.com'), $form->getData());
}

public function testResizedDownWithDeleteEmptyCallable()
{
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'entry_type' => AuthorType::class,
'allow_delete' => true,
'delete_empty' => function (Author $obj = null) {
return null === $obj || empty($obj->firstName);
},
));

$form->setData(array(new Author('Bob'), new Author('Alice')));
$form->submit(array(array('firstName' => 'Bob'), array('firstName' => '')));

$this->assertTrue($form->has('0'));
$this->assertFalse($form->has('1'));
$this->assertEquals(new Author('Bob'), $form[0]->getData());
$this->assertEquals(array(new Author('Bob')), $form->getData());
}

public function testResizedDownIfSubmittedWithCompoundEmptyDataDeleteEmptyAndNoDataClass()
{
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'entry_type' => AuthorType::class,
// If the field is not required, no new Author will be created if the
// form is completely empty
'entry_options' => array('data_class' => null),
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => function ($author) {
return empty($author['firstName']);
},
));
$form->setData(array(array('firstName' => 'first', 'lastName' => 'last')));
$form->submit(array(
array('firstName' => 's_first', 'lastName' => 's_last'),
array('firstName' => '', 'lastName' => ''),
));
$this->assertTrue($form->has('0'));
$this->assertFalse($form->has('1'));
$this->assertEquals(array('firstName' => 's_first', 'lastName' => 's_last'), $form[0]->getData());
$this->assertEquals(array(array('firstName' => 's_first', 'lastName' => 's_last')), $form->getData());
}

public function testDontAddEmptyDataIfDeleteEmpty()
{
$form = $this->factory->create(static::TESTED_TYPE, null, array(
Expand Down