我有以下表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type', ChoiceType::class, array(
'expanded' => true,
'multiple' => false,
'choices' => array(
'Friend' => 'friend',
'Guide' => 'guide'
)
));
}
如何在呈现表单时默认选中“朋友”复选框?
解决方法:
我认为你应该尝试使用数据选项,但它只是在你甚至没有在对象中保存数据的情况下,因为它会覆盖它.
重要提示:这对创建操作很有用,但不适用于编辑操作.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type', ChoiceType::class, array(
'expanded' => true,
'multiple' => false,
'choices' => array(
'Friend' => 'friend',
'Guide' => 'guide'
),
'data' => 'friend'
));
}
提取:
When you create a form, each field initially displays the value of the
corresponding property of the form’s domain object (if an object is
bound to the form). If you want to override the initial value for the
form or just an individual field, you can set it in the data option
更新如果你需要空的价值:
如下所示,如果您在任何情况下需要更新默认值,请使用empty_data替换数据
标签:php,symfony,symfony3-x
来源: https://codeday.me/bug/20191002/1843196.html