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 d479adf

Browse filesBrowse files
committed
[Form] fix empty_data option in expanded ChoiceType
1 parent fad545a commit d479adf
Copy full SHA for d479adf

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.