bug #17822 [WIP] [2.7] [Form] fix empty_data
option in expanded ChoiceType
(HeahDude)
This PR was merged into the 2.7 branch.
Discussion
----------
[WIP] [2.7] [Form] fix `empty_data` option in expanded `ChoiceType`
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #17791
| License | MIT
| Doc PR | -
It might happen because in `Form::submit()` the handling of `empty_data` [line 597](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Form.php#L597) comes after each child of a compound field has been submitted [line 549](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Form.php#L549).
So when `ChoiceType` is `expanded`, `compound` option is defaulted to `true` and it passes its empty submitted data to its children before handling its own `empty_data` option.
This PR uses the listener already added in `ChoiceType` only when `expanded` is true to handle `empty_data` at `PRE_SUBMIT` [line 539](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Form.php#L539).
- [ ] Fix FQCN in tests for 2.8
- [ ] Remove `choices_as_values` in tests for 3.0
Commits
-------
d479adf
[Form] fix `empty_data` option in expanded `ChoiceType`
This commit is contained in:
commit
7dbd849d0f
@ -33,6 +33,7 @@ use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface as Lega
|
||||
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer;
|
||||
use Symfony\Component\Form\Util\FormUtil;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
@ -90,6 +91,14 @@ class ChoiceType extends AbstractType
|
||||
$form = $event->getForm();
|
||||
$data = $event->getData();
|
||||
|
||||
if (null === $data) {
|
||||
$emptyData = $form->getConfig()->getEmptyData();
|
||||
|
||||
if (false === FormUtil::isEmpty($emptyData) && array() !== $emptyData) {
|
||||
$data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the submitted data to a string, if scalar, before
|
||||
// casting it to an array
|
||||
if (!is_array($data)) {
|
||||
|
@ -672,6 +672,66 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
}
|
||||
|
||||
public function testSubmitSingleChoiceWithEmptyData()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
'expanded' => false,
|
||||
'choices' => array('test'),
|
||||
'choices_as_values' => true,
|
||||
'empty_data' => 'test',
|
||||
));
|
||||
|
||||
$form->submit(null);
|
||||
|
||||
$this->assertSame('test', $form->getData());
|
||||
}
|
||||
|
||||
public function testSubmitMultipleChoiceWithEmptyData()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => true,
|
||||
'expanded' => false,
|
||||
'choices' => array('test'),
|
||||
'choices_as_values' => true,
|
||||
'empty_data' => array('test'),
|
||||
));
|
||||
|
||||
$form->submit(null);
|
||||
|
||||
$this->assertSame(array('test'), $form->getData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleChoiceExpandedWithEmptyData()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
'expanded' => true,
|
||||
'choices' => array('test'),
|
||||
'choices_as_values' => true,
|
||||
'empty_data' => 'test',
|
||||
));
|
||||
|
||||
$form->submit(null);
|
||||
|
||||
$this->assertSame('test', $form->getData());
|
||||
}
|
||||
|
||||
public function testSubmitMultipleChoiceExpandedWithEmptyData()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => true,
|
||||
'expanded' => true,
|
||||
'choices' => array('test'),
|
||||
'choices_as_values' => true,
|
||||
'empty_data' => array('test'),
|
||||
));
|
||||
|
||||
$form->submit(null);
|
||||
|
||||
$this->assertSame(array('test'), $form->getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
|
Reference in New Issue
Block a user