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
[Form] Added delete_empty option to allow proper emptyData handling of collections #9773
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,12 +45,18 @@ class ResizeFormListener implements EventSubscriberInterface | |
*/ | ||
protected $allowDelete; | ||
|
||
public function __construct($type, array $options = array(), $allowAdd = false, $allowDelete = false) | ||
/** | ||
* @var bool | ||
*/ | ||
private $deleteEmpty; | ||
|
||
public function __construct($type, array $options = array(), $allowAdd = false, $allowDelete = false, $deleteEmpty = false) | ||
{ | ||
$this->type = $type; | ||
$this->allowAdd = $allowAdd; | ||
$this->allowDelete = $allowDelete; | ||
$this->options = $options; | ||
$this->deleteEmpty = $deleteEmpty; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
|
@@ -126,8 +132,13 @@ public function preSubmit(FormEvent $event) | |
public function onSubmit(FormEvent $event) | ||
{ | ||
$form = $event->getForm(); | ||
$previousData = $event->getForm()->getData(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is "one time use", IMO it should go to inside |
||
$data = $event->getData(); | ||
|
||
// At this point, $data is an array or an array-like object that already contains the | ||
// new entries, which were added by the data mapper. The data mapper ignores existing | ||
// entries, so we need to manually unset removed entries in the collection. | ||
|
||
if (null === $data) { | ||
$data = array(); | ||
} | ||
|
@@ -136,10 +147,23 @@ public function onSubmit(FormEvent $event) | |
throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)'); | ||
} | ||
|
||
if ($this->deleteEmpty) { | ||
foreach ($form as $name => $child) { | ||
$isNew = !isset($previousData[$name]); | ||
|
||
// $isNew can only be true if allowAdd is true, so we don't | ||
// need to check allowAdd again | ||
if ($child->isEmpty() && ($isNew || $this->allowDelete)) { | ||
unset($data[$name]); | ||
$form->remove($name); | ||
} | ||
} | ||
} | ||
|
||
// The data mapper only adds, but does not remove items, so do this | ||
// here | ||
if ($this->allowDelete) { | ||
foreach ($data as $name => $child) { | ||
foreach ($data as $name => $childData) { | ||
if (!$form->has($name)) { | ||
unset($data[$name]); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,8 @@ public function buildForm(FormBuilderInterface $builder, array $options) | |
$options['type'], | ||
$options['options'], | ||
$options['allow_add'], | ||
$options['allow_delete'] | ||
$options['allow_delete'], | ||
$options['delete_empty'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @peterrehm There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is not an array that would break the code. |
||
); | ||
|
||
$builder->addEventSubscriber($resizeListener); | ||
|
@@ -86,6 +87,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) | |
'prototype_name' => '__name__', | ||
'type' => 'text', | ||
'options' => array(), | ||
'delete_empty' => false, | ||
)); | ||
|
||
$resolver->setNormalizers(array( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boolean