目前我有一个表单,它从我的类别实体中生成一个下拉选择:
$builder
->add('category', 'entity',
array('class' => 'TrackerMembersBundle:Category',
'property' => 'title',));
现在我想改为生成复选框,我可以在其中选择多个选项...我搜索了 symfony2 文档,但找不到直接从我的实体中执行此操作的简单方法。有什么想法吗?
最佳答案
将 'multiple' => true
添加到选项数组(class 和 property 所在的最后一个)给你一个多选。
然后您可以使用 form theming 覆盖 choice_widget block .
这样的事情可能会起作用:
{% block choice_widget %}
{% spaceless %}
{% for choice, label in choices %}
<label>
<input type="checkbox" value="{{ choice }}"{% if _form_is_choice_selected(form, choice) %} selected="selected"{% endif %}>
{{ label|trans }}
</label>
{% endfor %}
{% endspaceless %}
{% endblock choice_widget %}
关于forms - Symfony2 表格 : Generating checkboxes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9466293/