From dbaddbb7a89e404d97cc7bdaccdd16570b96d01b Mon Sep 17 00:00:00 2001 From: Thomas Chmielowiec Date: Tue, 7 Feb 2012 21:53:27 +0100 Subject: [PATCH] [Form] Allow empty choices array for ChoiceType --- .../Component/Form/Extension/Core/Type/ChoiceType.php | 2 +- .../Component/Form/Extension/Core/Type/ChoiceTypeTest.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index 394462150d..03beff4d31 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -37,7 +37,7 @@ class ChoiceType extends AbstractType throw new FormException('The "choice_list" must be an instance of "Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface".'); } - if (!$options['choice_list'] && !$options['choices']) { + if (!$options['choice_list'] && !is_array($options['choices']) && !$options['choices'] instanceof \Traversable) { throw new FormException('Either the option "choices" or "choice_list" must be set.'); } diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/ChoiceTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/ChoiceTypeTest.php index e30839a041..01ea4bf911 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/ChoiceTypeTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/ChoiceTypeTest.php @@ -662,4 +662,12 @@ class ChoiceTypeTest extends TypeTestCase $this->assertSame('name[]', $view->get('full_name')); } + + // https://github.com/symfony/symfony/issues/3298 + public function testInitializeWithEmptyChoices() + { + $this->factory->createNamed('choice', 'name', null, array( + 'choices' => array(), + )); + } }