Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: choice without empty value

Tags:

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?

like image 611
Rolintocour Avatar asked Mar 11 '15 16:03

Rolintocour


1 Answers

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

like image 149
Mikhail Prosalov Avatar answered Oct 22 '22 11:10

Mikhail Prosalov