Symfony2 具有不同选择的表单集合字段 [英] Symfony2 Form Collection Field with Different Choices
问题描述
我正在尝试根据数据库中的信息动态生成表单.根据我所在的类别页面,我需要显示一个选择框列表,每个选择框都有一组不同的选项.例如:
I'm trying to dynamically generate a form based on information from the database. Depending on the category page that I'm on, I need to display a list of select boxes, and each of those has a different set of options. For example:
<select name="attributes[1]">
<option value="foo">bar</option
<option value="foo2">bar2</option
<option value="foo3">bar3</option
</select>
<select name="attributes[2]">
<option value="fiz">buzz</option
<option value="fiz2">buzz2</option
<option value="fiz3">buzz3</option
</select>
我知道我必须使用 FormEvent 将字段添加到基于数据库数据的表单中.但是,我不知道如何添加一个集合字段,该字段包含集合的每个成员"的不同选择.我需要使用子表单吗?
I understand that I have to use a FormEvent to add fields to the form based on database data. However, I don't know how to add a collection field which contains different choices for each "member" of the collection. Do I need to use subforms?
推荐答案
您可能想要构建一个包含来自另一个对象的字段的表单.The Symfony Book 中有一节关于嵌入式表单(嵌入式表单) 和 CookBook 中一个不错的综合条目(如何嵌入表单集合) 涵盖更复杂的主题.
You probably want to build a form that includes fields from another object. There's a section in The Symfony Book about embedded forms (Embedded Forms) and a nice, comprehensive entry in the CookBook (How to Embed a Collection of Forms) covering even more complex topic.
这篇关于Symfony2 具有不同选择的表单集合字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!