Tek
Tek

Reputation: 3050

Extraneous option field in Symfony2 form

I'm using Symfony version 2.5.4 and I have a builder that looks like this.

$builder->add('Licensed', 'choice', array(
    'choices' => array(
        'N/A' => '--',
        'Yes' => 'Yes',
        'No' => 'No'
    ),
    'required' => false,
    'label' => 'Somelabel',
    'label_attr' => array('class' => 'font-bold'),
    'attr' => array('class' => 'rounded')
    )
);

When I render the form I get:

<select id="companyotherinformation_Licensed" name="companyotherinformation[Licensed]" class="rounded">
        <option value=""></option> <-- Not sure where this is coming from.
        <option value="N/A">--</option>
        <option value="Yes">Yes</option>
        <option value="No">No</option>
</select>

There's an <option value=""></option> but I have no idea how it got there. Where can I look to find out where it came about and get rid of it? Could it be a Symfony2 bug?

I've also tried app/console cache:clear and I'm still not able to get rid of it.

Upvotes: 0

Views: 51

Answers (1)

Cerad
Cerad

Reputation: 48865

Not a bug:

http://symfony.com/doc/current/reference/forms/types/choice.html#empty-value

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:

So add: 'empty_value' => false

But think about it, setting required = false implies that that the user does not have to select anything. But with your options, they have no choice. So having required = true would make more sense.

Upvotes: 1

Related Questions