Symfony2: choice without empty value

Rolintocour picture Rolintocour · Mar 11, 2015 · Viewed 18k times · Source

It seems to be easy but I cannot succeed into doing it. I have choices element. I want them to display only the value I set and not the default value (or empty value).

How can I achieve this?

Answer

Mikhail Prosalov picture Mikhail Prosalov · Mar 11, 2015

To disable empty value, try this:

$builder->add('states', 'choice', array(
    'empty_value' => false,
));

If you leave the empty_value option unset, then a blank (with no text) option will automatically be added if and only if the required option is false.

For Symfony 2.6+ please use 'placeholder' => false option to avoid empty value.

$builder->add('states', 'choice', array(
    'placeholder' => false,
));

http://symfony.com/doc/current/reference/forms/types/choice.html#placeholder