我正在使用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')
)
);
当我渲染表格时,我得到了:
<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
而我仍然无法摆脱它。
答案 0 :(得分: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'=&gt;假
但是考虑一下,设置required = false意味着用户不必选择任何东西。但是根据您的选择,他们别无选择。所以有必要= true会更有意义。