[英]Symfony2 embedded forms using one class
Is it possible to use an Embedded form without using a separated class for it? 是否可以使用嵌入式表单而不使用单独的类? The reason is I've got a lot of form classes already, which most of the time contain a single field, so I wonder if it is possible to define embedded forms inline.
原因是我已经有很多表单类,大多数时候它们都包含一个字段,所以我想知道是否可以内联定义嵌入式表单。
So normally we have something like this: 所以通常我们有这样的东西:
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name')
->add('email')
->add('phone')
->add('key', new KeyType())
;
}
The documentation says that I have to create a class for the key
field, KeyType
for example, where I would set up the form builder for the embedded form. 该文档说,我必须为
key
字段创建一个类,例如KeyType
,在这里我将为嵌入式表单设置表单生成器。 But I would like to, instead of creating the class KeyType
, define the fields inline, in the same class. 但我想,而不是创建
KeyType
类,而是在同一类中内联定义字段。 How can I do that? 我怎样才能做到这一点?
Yes this is possible. 是的,这是可能的。
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name')
->add('email')
->add('phone')
->add(
$builder->create('key')
->add('someField', 'text')
->add('otherField', 'checkbox')
)
;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.