【发布时间】:2015-11-09 10:07:11
【问题描述】:
我的表单类型如下:
class TestType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, $options){
$builder->add('type', 'text');
$builder->add('name', 'text');
}
}
现在我想创建一个表单类型,其中包含两个 TestTypes,但只有一个 'type' 字段用于其中使用的两个 TestType 字段:
class DoubleTestType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, $options){
$builder->add('first', new TestType());
$builder->add('second', new TestType()); // however this will create one type 'field' for each of the SubForms
}
}
这种方法将创建两个TestTypes,每个都有一个单独的'type' 字段,但我希望它们之间共享'type' 字段。一种方法是在DoubleTestType 中创建一个'type' 字段,并隐藏TestTypes 中的type 字段,并使用在DoubleTestType 上注册的事件侦听器设置它们的值。我正在寻找一种更清洁的方法来做到这一点。
【问题讨论】: