使用表单生成器 symfony2 创建数组输入字段 [英] Create array input field with form builder symfony2
问题描述
我在 Symfony2 中使用表单构建器时遇到问题.确切地说,我需要输入字段是 html 数组,但我无法使用 createFormBuilder->add 创建它.这是我尝试过的:
$attributesForm = $this->createFormBuilder()->add('attribute[0]', 'text') ...
依此类推,但我收到以下异常:
<块引用>名称属性[0]"包含非法字符.名称应以字母、> 数字或下划线开头,并且只能包含字母、数字、数字、下划线 ("_")、连字符 >("-") 和冒号 (":").
有什么好的解决方案,或者我必须手动创建字段?
提前致谢!
进一步澄清这一点......我想要生成这样的东西:
<div>名称<input type="text" name="name"></div><div>...</div><div>属性 0 <input type="text" name="attribute[0]"></div><div>属性1<input type="text" name="attribute[1]"></div><div>属性3<input type="text" name="attribute[3]"></div><ul><p><button type="submit">编辑</button></p>帮助?
解决方案 您可以使用集合"字段类型创建输入字段数组.
可以在此处找到有关如何使用它的文档:
如果这还不够清楚,或者您仍有疑问,我很乐意为您提供帮助.
I'm having a trouble with using Form builder in Symfony2.
To be exact, I need input field that is html array, but I can't create it with createFormBuilder->add.
Here is what I tried:
$attributesForm = $this->createFormBuilder()
->add('attribute[0]', 'text') ...
And so on, but I get the following exception:
The name "attribute[0]" contains illegal characters. Names should start with a letter, >digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens >("-") and colons (":").
Is there any nice solution or I have to create fields manually?
Thanks in advance!
EDIT: to clarify this further...
I want something like this to be generated:
<div id="msoft_adminbundle_offertype">
<div>Name <input type="text" name="name"></div>
<div>...</div>
<div>Attribute 0 <input type="text" name="attribute[0]"></div>
<div>Attribute 1 <input type="text" name="attribute[1]"></div>
<div>Attribute 3 <input type="text" name="attribute[3]"></div>
<ul>
</ul>
<p>
<button type="submit">Edit</button>
</p>
Help?
解决方案 You can create an array of input fields using the 'collection'-field type.
Documentation about how to use it can be found here:
If that isn't clear enough or you still have questions I will gladly help you with them.
这篇关于使用表单生成器 symfony2 创建数组输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文