[Form] Fix 5.5 compatibility for ResizeFormListener

This commit is contained in:
Yonel Ceruto 2017-10-12 18:51:51 -04:00 committed by Fabien Potencier
parent f35996d228
commit ee70361e53
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());
}
}