2014-09-19 60 views
0

我使用的是Symfony版本2.5.4,我有一个看起来像这样的构建器。Symfony2中的外部选项字段

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

当我呈现的形式获得:

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

有一个<option value=""></option>,但我不知道它是如何到达那里。我可以在哪里查找它的来源并摆脱它?它可能是一个Symfony2错误?我也尝试过app/console cache:clear,我仍然无法摆脱它。

回答

1

不是一个错误:

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: 

所以加: 'empty_value'=>假

但仔细想想,设置所需= FALSE意味着该用户没有选择任何东西。但随着你的选择,他们别无选择。所以有required = true会更有意义。

+0

太好了,谢谢你的提供。 – Tek 2014-09-22 14:36:29