Skip to content

Commit

Permalink
Merge branch '3.4'
Browse files Browse the repository at this point in the history
* 3.4:
  [TwigBridge] fix BC for FormExtension if renderer is FormRenderer
  [Form] Fix 5.5 compatibility for ResizeFormListener
  [BrowserKit] Handle deprecations triggered in insulated requests
  [Bridge\PhpUnit] Handle deprecations triggered in separate processes
  Fix LogLevel::DEBUG as min level
  [Validator] added magic method __isset()  to File Constraint class
  Support array of types in allowed type
  [DI] Fix possible incorrect php-code when dumped strings contains newlines
  [Translation] minor: remove unused variable in test
  added ability to handle parent classes for PropertyNormalizer
  replace parameters in dummy identity translator
  never match invalid IP addresses
  • Loading branch information
nicolas-grekas committed Oct 13, 2017
2 parents 7fdcf54 + 00d3172 commit 9005c6a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Extension/Core/EventListener/ResizeFormListener.php
Expand Up @@ -145,12 +145,12 @@ public function onSubmit(FormEvent $event)
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
Expand Down
47 changes: 47 additions & 0 deletions Tests/Extension/Core/EventListener/ResizeFormListenerTest.php
Expand Up @@ -275,4 +275,51 @@ public function testOnSubmitDealsWithArrayBackedIteratorAggregate()
$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());
}
}

0 comments on commit 9005c6a

Please sign in to comment.