8000 merged branch chmielot/ticket_3298 (PR #3299) · symfony/symfony@2ae1f90 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ae1f90

Browse files
committed
merged branch chmielot/ticket_3298 (PR #3299)
Commits ------- dbaddbb [Form] Allow empty choices array for ChoiceType Discussion ---------- [Form] Allow empty choices array for ChoiceType Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes The documentation about ChoiceType says, that default for 'choices' is array(). This is not allowed because an empty array evaluates to false: if (!$options['choice_list'] && !$options['choices']) { Use case: choices are empty and items are added dynamically. --------------------------------------------------------------------------- by chmielot at 2012-02-07T21:03:03Z Sorry, I messed up with the tickets. Didn't know a pull request opens a ticket. --------------------------------------------------------------------------- by bschussek at 2012-02-08T08:23:29Z This ticket depends on #3290 for being merged. Apart from this, a test case is missing. Add this to ChoiceTypeTest: // #3298 public function testInitializeWithEmptyChoices() { $this->factory->createNamed('choice', 'name', null, array( 'choices' => array(), )); } --------------------------------------------------------------------------- by craue at 2012-02-10T20:32:44Z @bschussek: Why does it depend on #3290? I have issues with the `!$options['choices']` check even without #3290 applied. --------------------------------------------------------------------------- by chmielot at 2012-02-10T21:24:28Z Ok, I updated the branch with the test case and craue's suggestion on explicitly checking valid values. --------------------------------------------------------------------------- by chmielot at 2012-02-11T09:07:05Z Should be fine now. --------------------------------------------------------------------------- by bschussek at 2012-02-11T09:15:10Z Good. I see that you added a check for \Traversable too - can you add a test for this too? It should be fine to pass an empty \ArrayObject(). --------------------------------------------------------------------------- by craue at 2012-02-11T10:05:36Z @bschussek: But even if there's passed something different than an array, the c'tor of `SimpleChoiceList` (which is called in line 45) is type hinted with `array` and won't allow passing a `Traversable` anyway, right? --------------------------------------------------------------------------- by bschussek at 2012-02-11T10:16:36Z @craue Yes. But in EntityType the choices option is reused and passed to EntityChoiceList, which extends ChoiceList and accepts any array or Traversable. You're right though that it's not possible to add a test covering this in ChoiceTypeTest, and in EntityTypeTest this is already covered. @fabpot Ready to merge.
2 parents 92cb685 + dbaddbb commit 2ae1f90

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function buildForm(FormBuilder $builder, array $options)
3737
throw new FormException('The "choice_list" must be an instance of "Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface".');
3838
}
3939

40-
if (!$options['choice_list'] && !$options['choices']) {
40+
if (!$options['choice_list'] && !is_array($options['choices']) && !$options['choices'] instanceof \Traversable) {
4141
throw new FormException('Either the option "choices" or "choice_list" must be set.');
4242
}
4343

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,4 +662,12 @@ public function testAdjustFullNameForMultipleNonExpanded()
662662

663663
$this->assertSame('name[]', $view->get('full_name'));
664664
}
665+
666+
// https://github.com/symfony/symfony/issues/3298
667+
public function testInitializeWithEmptyChoices()
668+
{
669+
$this->factory->createNamed('choice', 'name', null, array(
670+
'choices' => array(),
671+
));
672+
}
665673
}

0 commit comments

Comments
 (0)
0