Symfony2 Beta 上如何处理 Form Collection?
我有一个实体用户和一个实体地址。 User 和 Address 之间存在一对多关系:
class User
{
/**
* @orm:OneToMany(targetEntity="Address")
*/
protected $adresses;
[...]
}
我有一个类 AddressType 和类 UserType :
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('addresses', 'collection', array('type' => new AddressType()));
}
[...]
}
在我的控制器中,我使用 :
$form = $this->get('form.factory')->create(new UserType());
... 构建表单并使用 : 创建视图:
return array('form' => $form->createView());
我在我的树枝模板中显示表单字段:
{{ form_errors(form.name) }}
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
[...]
好的。现在,如何显示一个或多个地址的字段? (不是 {{ for_widget(form.adresses.zipcode) }}
也不是 {{ for_widget(form.adresses[0].zipcode) }}
...)
任何想法?
I have an entity User and an entity Address. There is a relation One-to-Many between User and Address :
class User
{
/**
* @orm:OneToMany(targetEntity="Address")
*/
protected $adresses;
[...]
}
I have a class AddressType, and class UserType :
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('addresses', 'collection', array('type' => new AddressType()));
}
[...]
}
In my controller, I build form with :
$form = $this->get('form.factory')->create(new UserType());
... and create view with :
return array('form' => $form->createView());
I display form field in my twig template with :
{{ form_errors(form.name) }}
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
[...]
Okay. Now, how to display fields for one or more addresses ? (it's no {{ for_widget(form.adresses.zipcode) }}
nor {{ for_widget(form.adresses[0].zipcode) }}
...)
Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我在表单模板中的做法:
我有一个由 jQuery 驱动的小操作栏,允许用户添加和删除地址。这是一个简单的脚本,使用正确的 HTML 代码将新的 div 附加到容器。对于 HTML,我只是使用了 Symfony 的相同输出,但更新了索引。例如,这将是
AddressType
表单的街道输入文本的输出:然后,Symfony 将接受的下一个索引是 1,因此您添加的新输入字段将如下所示:
注意:这三个点是
required="required" maxlength="255"
的替代,但可能会根据您的需要进行更改。您将需要更多的 HTML 代码来将全新的
AddressType
添加到浏览器的 DOM,但这给了您总体思路。问候,
马特
This is how I did it in my form template:
And I have a small action bar, driven by jQuery, that lets the user add and remove addresses. It is a simple script appending a new div to the container with the right HTML code. For the HTML, I just used the same output has Symfony but with updated index. For example, this would be the output for the street input text of the
AddressType
form:<input id="user_addresses_0_street" name="user[addresses][0][street]" ...>
Then, the next index Symfony will accept is 1 so the new input field you add would look like this:
<input id="user_addresses_1_street" name="user[addresses][1][street]" ...>
Note: The three dots are a remplacement for
required="required" maxlength="255"
but could change depending on your needs.You will need more HTML code than that to add a whole new
AddressType
to the DOM of the browser but this give you the general idea.Regards,
Matt
我应该补充一点,如果你想动态添加字段,你需要在 UserType 的集合字段中将键 'allow_add' 设置为 true :
刚刚花了几个小时试图找出缺少的内容,并且当时我正在写的文档还没有提到这一点。希望对各位开发者有所帮助。
I should top that up with the fact that if you want to dynamically add fields, you need to set the key 'allow_add' to true in your collection field in UserType :
Just spent hours trying to figure out what was missing, and at the time i'm writing the doc does not mention this yet. Hope it'll help fellow developers.