简体   繁体   English

Symfony2-将一种形式绑定到多个对象

[英]Symfony2 - bind one form to multiple objects

I'm currently creating a form, something along the lines of: 我目前正在创建一个表单,大致类似于:

$myDto = // ... etc - I am not using Doctrine

$form = $this->createForm(new DtoType(), $myDto);
$form->bind($request);

The values get bound to the form and end up assigned to $myDto. 这些值绑定到表单并最终分配给$ myDto。 That's fine. 没关系。

I would like to prepend a couple of extra fields to my form for a different object . 我想在表单中为其他对象添加几个额外的字段。

I could create a "composite" type which contains them both - I could call it CompositeFormType which contains DtoType (the original form) and ActionsType (the new fields), a bit like described here http://symfony.com/doc/current/book/forms.html#embedded-forms . 我可以创建一个包含它们两者的“复合”类型-我可以将其称为CompositeFormType ,其中包含DtoType (原始表单)和ActionsType (新字段),有点像此处描述的http://symfony.com/doc/current /book/forms.html#embedded-forms

If that's the best way, then once I have created this CompositeFormType , how would I get the values in/out of the form? 如果那是最好的方法,那么一旦创建了CompositeFormType ,如何将值输入/输出表格? What would I pass to 'createForm'? 我将如何传递给“ createForm”? So for example: 因此,例如:

$myDto =   // ... etc
$actions = // ... etc

$form = $this->createForm(new CompositeFormType(), ???);

                  //                     What here? ^
                  //
                  // I want both 'actions' and 'myDto' to be populated by
                  // the child types? How?

$form->bind($request);

... or should I be doing it a completely different way? ...还是应该以一种完全不同的方式来做? Thanks in advance for your pointers. 预先感谢您的指导。

您可以创建一个同时拥有两个对象作为属性的新类,并将该类的实例传递给此复合形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM