如何使用 symfony2 表单生成器呈现默认选中的复选框? [英] How to render a checkbox that is checked by default with the symfony2 Form Builder?

查看:12
本文介绍了如何使用 symfony2 表单生成器呈现默认选中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到任何简单的方法来完成默认情况下简单地选中复选框.那不可能那么难,那我错过了什么?

I have not found any easy way to accomplish to simply check a Checkbox by default. That can not be that hard, so what am i missing?

推荐答案

您只需将模型或实体中的值设置为 true,然后将其传递给 FormBuilder,然后就应该对其进行检查.

You would simply set the value in your model or entity to true and than pass it to the FormBuilder then it should be checked.

如果您查看文档中的第一个示例:

If you have a look at the first example in the documentation:

创建一个新任务,然后执行 setTask 并将此任务添加到 FormBuilder.如果你对你的复选框做同样的事情

A new task is created, then setTask is executed and this task is added to the FormBuilder. If you do the same thing with your checkbox

$object->setCheckboxValue(true);

并传递对象,您应该看到复选框被选中.

and pass the object you should see the checkbox checked.

如果它没有按预期工作,请返回一些重现错误的示例代码.

If it's not working as expected, please get back with some sample code reproducing the error.

这篇关于如何使用 symfony2 表单生成器呈现默认选中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆