8000 [Form] fix `empty_data` option in expanded `ChoiceType` · symfony/symfony@b4858c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit b4858c7

Browse files
committed
[Form] fix empty_data option in expanded ChoiceType
1 parent 1c79c7b commit b4858c7

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

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

Lines 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

@@ -92,6 +93,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
9293
$form = $event->getForm();
9394
$data = $event->getData();
9495

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

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,66 @@ public function testSubmitSingleNonExpandedObjectChoices()
488488
$this->assertTrue($form->isSynchronized());
489489
}
490490

491+
public function testSubmitSingleChoiceWithEmptyData()
492+
{
493+
$form = $this->factory->create('choice', null, array(
494+
'multiple' => false,
495+
'expanded' => false,
496+
'choices' => array('test'),
497+
'choices_as_values' => true,
498+
'empty_data' => 'test',
499+
));
500+
501+
$form->submit(null);
502+
503+
$this->assertSame('test', $form->getData());
504+
}
505+
506+
public function testSubmitMultipleChoiceWithEmptyData()
507+
{
508+
$form = $this->factory->create('choice', null, array(
509+
'multiple' => true,
510+
'expanded' => false,
511+
'choices' => array('test'),
512+
'choices_as_values' => true,
513+
'empty_data' => array('test'),
514+
));
515+
516+
$form->submit(null);
517+
518+
$this->assertSame(array('test'), $form->getData());
519+
}
520+
521+
public function testSubmitSingleChoiceExpandedWithEmptyData()
522+
{
523+
$form = $this->factory->create('choice', null, array(
524+
'multiple' => false,
525+
'expanded' => true,
526+
'choices' => array('test'),
527+
'choices_as_values' => true,
528+
'empty_data' => 'test',
529+
));
530+
531+
$form->submit(null);
532+
533+
$this->assertSame('test', $form->getData());
534+
}
535+
536+
public function testSubmitMultipleChoiceExpandedWithEmptyData()
537+
{
538+
$form = $this->factory->create('choice', null, array(
539+
'multiple' => true,
540+
'expanded' => true,
541+
'choices' => array('test'),
542+
'choices_as_values' => true,
543+
'empty_data' => array('test'),
544+
));
545+
546+
$form->submit(null);
547+
548+
$this->assertSame(array('test'), $form->getData());
549+
}
550+
491551
/**
492552
* @group legacy
493553
*/

0 commit comments

Comments
 (0)
0