php选项框默认值,php – 选择字段默认值

在Symfony中创建表单时,可以通过设置`data`选项来指定字段的默认值。例如,在`buildForm`方法中,为`ChoiceType`字段添加`data`选项可以预选中'朋友'选项。然而,这种方法只适用于创建操作,不适用于编辑操作,因为数据可能会被覆盖。如果需要为空值,可以使用`empty_data`选项替代。
摘要由CSDN通过智能技术生成

我有以下表格:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值