2014-09-19 11 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