Roubi
Roubi

Reputation: 2106

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

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") ?

Upvotes: 6

Views: 7218

Answers (2)

Mohammed Zayan
Mohammed Zayan

Reputation: 899

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
));

Upvotes: 24

yceruto
yceruto

Reputation: 9585

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)

Upvotes: 16

Related Questions