Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] Allow empty choices array for ChoiceType #3299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2012

Conversation

chmielot
Copy link

@chmielot chmielot commented Feb 7, 2012

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.

@chmielot
Copy link
Author

chmielot commented Feb 7, 2012

Sorry, I messed up with the tickets. Didn't know a pull request opens a ticket.

@webmozart
Copy link
Contributor

This ticket depends on #3290 for being merged.

Apart from this, a test case is missing. Add this to ChoiceTypeTest:

// https://github.com/symfony/symfony/issues/3298
public function testInitializeWithEmptyChoices()
{
    $this->factory->createNamed('choice', 'name', null, array(
        'choices' => array(),
    ));
}

@@ -37,7 +37,7 @@ public function buildForm(FormBuilder $builder, array $options)
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'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should do a null-check instead.

null === $options['choices']

In some cases, choices might be a traversable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false should also not be allowed. Maybe it's better to explicitly check for valid values:

if (!$options['choice_list'] && !is_array($options['choices']) && !$options['choices'] instanceof \Traversable) {

👍 for getting this fixed soon.

@craue
Copy link
Contributor

craue commented Feb 10, 2012

@bschussek: Why does it depend on #3290? I have issues with the !$options['choices'] check even without #3290 applied.

@chmielot
Copy link
Author

Ok, I updated the branch with the test case and craue's suggestion on explicitly checking valid values.

@@ -662,4 +662,11 @@ public function testAdjustFullNameForMultipleNonExpanded()

$this->assertSame('name[]', $view->get('full_name'));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the URL of the ticket here please?

// https://github.com/symfony/symfony/issues/3298

@chmielot
Copy link
Author

Should be fine now.

@webmozart
Copy link
Contributor

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().

@craue
Copy link
Contributor

craue commented Feb 11, 2012

@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?

@webmozart
Copy link
Contributor

@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.

fabpot added a commit that referenced this pull request Feb 11, 2012
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.
@fabpot fabpot merged commit dbaddbb into symfony:master Feb 11, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.