Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7dbd849

Browse filesBrowse files
committed
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`
2 parents d8dd7f6 + d479adf commit 7dbd849
Copy full SHA for 7dbd849

File tree

2 files changed

+69
-0
lines changed
Filter options

2 files changed

+69
-0
lines changed

‎src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
3434
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer;
3535
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer;
36+
use Symfony\Component\Form\Util\FormUtil;
3637
use Symfony\Component\OptionsResolver\Options;
3738
use Symfony\Component\OptionsResolver\OptionsResolver;
3839

@@ -90,6 +91,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
9091
$form = $event->getForm();
9192
$data = $event->getData();
9293

94+
if (null === $data) {
95+
$emptyData = $form->getConfig()->getEmptyData();
96+
97+
if (false === FormUtil::isEmpty($emptyData) && array() !== $emptyData) {
98+
$data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData;
99+
}
100+
}
101+
93102
// Convert the submitted data to a string, if scalar, before
94103
// casting it to an array
95104
if (!is_array($data)) {

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,66 @@ public function testSubmitSingleNonExpandedObjectChoices()
672672
$this->assertTrue($form->isSynchronized());
673673
}
674674

675+
public function testSubmitSingleChoiceWithEmptyData()
676+
{
677+
$form = $this->factory->create('choice', null, array(
678+
'multiple' => false,
679+
'expanded' => false,
680+
'choices' => array('test'),
681+
'choices_as_values' => true,
682+
'empty_data' => 'test',
683+
));
684+
685+
$form->submit(null);
686+
687+
$this->assertSame('test', $form->getData());
688+
}
689+
690+
public function testSubmitMultipleChoiceWithEmptyData()
691+
{
692+
$form = $this->factory->create('choice', null, array(
693+
'multiple' => true,
694+
'expanded' => false,
695+
'choices' => array('test'),
696+
'choices_as_values' => true,
697+
'empty_data' => array('test'),
698+
));
699+
700+
$form->submit(null);
701+
702+
$this->assertSame(array('test'), $form->getData());
703+
}
704+
705+
public function testSubmitSingleChoiceExpandedWithEmptyData()
706+
{
707+
$form = $this->factory->create('choice', null, array(
708+
'multiple' => false,
709+
'expanded' => true,
710+
'choices' => array('test'),
711+
'choices_as_values' => true,
712+
'empty_data' => 'test',
713+
));
714+
715+
$form->submit(null);
716+
717+
$this->assertSame('test', $form->getData());
718+
}
719+
720+
public function testSubmitMultipleChoiceExpandedWithEmptyData()
721+
{
722+
$form = $this->factory->create('choice', null, array(
723+
'multiple' => true,
724+
'expanded' => true,
725+
'choices' => array('test'),
726+
'choices_as_values' => true,
727+
'empty_data' => array('test'),
728+
));
729+
730+
$form->submit(null);
731+
732+
$this->assertSame(array('test'), $form->getData());
733+
}
734+
675735
/**
676736
* @group legacy
677737
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.