Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony: How to customize the "none" label appearing on radio field choices?

Tags:

forms

symfony

I'm using radio buttons with "required" => false. This makes a "none" option appear along with my other choices.

How can I customize this label (let's say I would want to display "null" instead of "none") ?

like image 340
Roubi Avatar asked Oct 24 '16 17:10

Roubi


2 Answers

If you want to remove it set placeholder to false

->add('color', ChoiceType::class, array(
    'choices' => array('Red' => 1, 'Blue' => 2, 'Green' => 3),
    'expanded' => true,
    'required' => false,
    'placeholder' => false
));
like image 50
Mohammed Zayan Avatar answered Oct 20 '22 15:10

Mohammed Zayan


Use placeholder option to change the label for "None" radio input:

->add('color', ChoiceType::class, array(
    'choices' => array('Red' => 1, 'Blue' => 2, 'Green' => 3),
    'expanded' => true,
    'required' => false,
    'placeholder' => 'Null', // <---- \o/
));

Preview:

none-radio-input

The placeholder option was introduced in Symfony 2.6 (doc)

like image 26
yceruto Avatar answered Oct 20 '22 16:10

yceruto