bug #24541 [Form] Fix 5.5 compatibility for ResizeFormListener (yceruto)

This PR was squashed before being merged into the 3.4 branch (closes #24541).

Discussion
----------

[Form] Fix 5.5 compatibility for ResizeFormListener

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

531b294b21/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php (L152-L157)

Issue description: http://php.net/manual/en/language.types.callable.php#117260 (https://3v4l.org/oOoAV)

Commits
-------

ee70361e53 [Form] Fix 5.5 compatibility for ResizeFormListener
This commit is contained in:
Fabien Potencier 2017-10-13 06:25:58 -07:00
commit f53229c236
2 changed files with 49 additions and 2 deletions

View File

@ -149,12 +149,12 @@ class ResizeFormListener implements EventSubscriberInterface
throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)');
}
if ($entryFilter = $this->deleteEmpty) {
if ($this->deleteEmpty) {
$previousData = $form->getData();
/** @var FormInterface $child */
foreach ($form as $name => $child) {
$isNew = !isset($previousData[$name]);
$isEmpty = is_callable($entryFilter) ? $entryFilter($child->getData()) : $child->isEmpty();
$isEmpty = is_callable($this->deleteEmpty) ? call_user_func($this->deleteEmpty, $child->getData()) : $child->isEmpty();
// $isNew can only be true if allowAdd is true, so we don't
// need to check allowAdd again

View File

@ -275,4 +275,51 @@ class ResizeFormListenerTest extends TestCase
$this->assertArrayNotHasKey(0, $event->getData());
$this->assertArrayNotHasKey(2, $event->getData());
}
public function testOnSubmitDeleteEmptyNotCompoundEntriesIfAllowDelete()
{
$this->form->setData(array('0' => 'first', '1' => 'second'));
$this->form->add($this->getForm('0'));
$this->form->add($this->getForm('1'));
$data = array(0 => 'first', 1 => '');
foreach ($data as $child => $dat) {
$this->form->get($child)->setData($dat);
}
$event = new FormEvent($this->form, $data);
$listener = new ResizeFormListener('text', array(), false, true, true);
$listener->onSubmit($event);
$this->assertEquals(array(0 => 'first'), $event->getData());
}
public function testOnSubmitDeleteEmptyCompoundEntriesIfAllowDelete()
{
$this->form->setData(array('0' => array('name' => 'John'), '1' => array('name' => 'Jane')));
$form1 = $this->getBuilder('0')
->setCompound(true)
->setDataMapper($this->getDataMapper())
->getForm();
$form1->add($this->getForm('name'));
$form2 = $this->getBuilder('1')
->setCompound(true)
->setDataMapper($this->getDataMapper())
->getForm();
$form2->add($this->getForm('name'));
$this->form->add($form1);
$this->form->add($form2);
$data = array('0' => array('name' => 'John'), '1' => array('name' => ''));
foreach ($data as $child => $dat) {
$this->form->get($child)->setData($dat);
}
$event = new FormEvent($this->form, $data);
$callback = function ($data) {
return '' === $data['name'];
};
$listener = new ResizeFormListener('text', array(), false, true, $callback);
$listener->onSubmit($event);
$this->assertEquals(array('0' => array('name' => 'John')), $event->getData());
}
}