From d479adf0735d240b52ec8ce2cde7af17e465906c Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Wed, 17 Feb 2016 04:19:28 +0100 Subject: [PATCH] [Form] fix `empty_data` option in expanded `ChoiceType` --- .../Form/Extension/Core/Type/ChoiceType.php | 9 +++ .../Extension/Core/Type/ChoiceTypeTest.php | 60 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index a61317de8522..9a67a049b180 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -33,6 +33,7 @@ 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 @@ public function buildForm(FormBuilderInterface $builder, array $options) $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)) { diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index a00fcacdb70b..b7bea7e5c806 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -672,6 +672,66 @@ public function testSubmitSingleNonExpandedObjectChoices() $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 */