8000 [Form] Choice Field doesn't render all choices · Issue #14947 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Choice Field doesn't render all choices #14947

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

Closed
timvermaercke opened this issue Jun 11, 2015 · 2 comments
Closed

[Form] Choice Field doesn't render all choices #14947

timvermaercke opened this issue Jun 11, 2015 · 2 comments

Comments

@timvermaercke
Copy link

Hi

I have a select box that has to contain the following options. Mention the same label for the options "audi" and "ford".

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
  <option value="ford">Audi</option>
</select>

When I try to render this in Symfony 2.7, my FormType looks like this.

$builder->add('brand', 'choice', array(
    'choices' => array(
        'volvo' => 'Volvo',
        'saab' => 'Saab',
        'opel' => 'Opel',
        'audi' => 'Audi',
        'ford' => 'Audi'
    )
));

I would assume all 5 fields are going to get rendered. In fact, only 4 get rendered and the view looks like this:

<select id="...">
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
   <option value="opel">Opel</option>
   <option value="ford">Audi</option>
</select>

It seems the option with value "audi" is overriden by the option with value "ford". I don't know if this is standard behaviour or a bug, but it's quite annoying. Can any of you help me?

Thanks in advance!

@webmozart
Copy link
Contributor

Hi @maercky, thank you for reporting this issue! I can confirm this bug. Investigating now.

@webmozart
Copy link
Contributor

I see the problem now. As of Symfony 2.7, the "choices" can and should be flipped:

$builder->add('brand', 'choice', array(
    'choices' => array(
        'Volvo' => 'volvo',
        'Saab' => 'saab',
        'Opel' => 'opel',
        'Audi' => 'audi',
        'Audi' => 'ford'
    ),
    'choices_as_values' => true,
));

Obviously, duplicate labels are not and will not be supported for this syntax.

In Symfony 2.7, if you don't set "choices_as_values" to true, Symfony will basically just array_flip your "choices" array - eliminating any duplicate labels. That's the source of your problem.

The value false for "choices_as_values" will be deprecated in Symfony 2.8 (#14951), so this won't and cannot be fixed.

As workaround, I recommend to set the "choice_label" option like this:

$labels = array(
    'volvo' => 'Volvo',
    'saab' => 'Saab',
    'opel' => 'Opel',
    'audi' => 'Audi',
    'ford' => 'Audi'
);

$builder->add('brand', 'choice', array(
    'choices' => array_keys($labels),
    // forward compatibility
    'choices_as_values' => true,
    'choice_label' => function ($choice) use ($labels) {
        return $labels[$choice];
    },
));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
0