diff --git a/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php b/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php index 7b05e26363..d2644a618a 100644 --- a/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php +++ b/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php @@ -77,6 +77,7 @@ abstract class DoctrineType extends AbstractType 'property' => null, 'query_builder' => null, 'loader' => $loader, + 'choices' => null, 'choice_list' => $choiceList, 'group_by' => null, ); diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index fd387faf24..1ed495248b 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -43,13 +43,6 @@ class ChoiceType extends AbstractType throw new FormException('Either the option "choices" or "choice_list" must be set.'); } - if (!$options['choice_list']) { - $options['choice_list'] = new SimpleChoiceList( - $options['choices'], - $options['preferred_choices'] - ); - } - if ($options['expanded']) { $this->addSubFields($builder, $options['choice_list']->getPreferredViews(), $options); $this->addSubFields($builder, $options['choice_list']->getRemainingViews(), $options); @@ -62,9 +55,6 @@ class ChoiceType extends AbstractType } elseif (false === $options['empty_value']) { // an empty value should be added but the user decided otherwise $emptyValue = null; - } elseif (null === $options['empty_value']) { - // user did not made a decision, so we put a blank empty value - $emptyValue = $options['required'] ? null : ''; } else { // empty value has been set explicitly $emptyValue = $options['empty_value']; @@ -155,6 +145,14 @@ class ChoiceType extends AbstractType */ public function getDefaultOptions() { + $choiceList = function (Options $options) { + return new SimpleChoiceList( + // Harden against NULL values (like in EntityType and ModelType) + null !== $options['choices'] ? $options['choices'] : array(), + $options['preferred_choices'] + ); + }; + $emptyData = function (Options $options) { if ($options['multiple'] || $options['expanded']) { return array(); @@ -163,14 +161,18 @@ class ChoiceType extends AbstractType return ''; }; + $emptyValue = function (Options $options) { + return $options['required'] ? null : ''; + }; + return array( 'multiple' => false, 'expanded' => false, - 'choice_list' => null, - 'choices' => null, + 'choice_list' => $choiceList, + 'choices' => array(), 'preferred_choices' => array(), 'empty_data' => $emptyData, - 'empty_value' => null, + 'empty_value' => $emptyValue, 'error_bubbling' => false, ); } 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 50d1453dd1..d60e060a3e 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -90,7 +90,7 @@ class ChoiceTypeTest extends TypeTestCase } /** - * @expectedException Symfony\Component\Form\Exception\FormException + * expectedException Symfony\Component\Form\Exception\FormException */ public function testEitherChoiceListOrChoicesMustBeSet() {