简体   繁体   English

Symfony2形式的无关选项字段

[英]Extraneous option field in Symfony2 form

I'm using Symfony version 2.5.4 and I have a builder that looks like this. 我正在使用Symfony 2.5.4版,并且有一个看起来像这样的生成器。

$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. 有一个<option value=""></option>但我不知道它是如何到达那里的。 Where can I look to find out where it came about and get rid of it? 我在哪里可以找到它的来龙去脉? Could it be a Symfony2 bug? 可能是Symfony2错误吗?

I've also tried app/console cache:clear and I'm still not able to get rid of it. 我也尝试过app/console cache:clear ,但仍然无法摆脱它。

Not a bug: 不是错误:

http://symfony.com/doc/current/reference/forms/types/choice.html#empty-value 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 因此添加:'empty_value'=> false

But think about it, setting required = false implies that that the user does not have to select anything. 但是考虑一下,将required设置为false意味着用户不必选择任何内容。 But with your options, they have no choice. 但是有了您的选择,他们别无选择。 So having required = true would make more sense. 因此,具有required = true会更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM