0
我创建了一个为实体添加自动完成功能的窗体类型,但它需要为每个实体进行一些配置,即:我必须将配置传递给选项数组,所以我决定使用我创建的AutoCompleteType为每个实体创建一个新的FormType,并重新使用它们。但是,我希望这些FormType(即每个特定实体的FormType)在调用getData()
时返回实体,现在发生的是我必须首先检索包含AutoCompleteType
的ParentForm字段,然后致电getData()
以检索我的实体。如何直接在ParentForm上映射此信息?在父窗体上映射子窗体的数据symfony2
//the FormType of Some Entity using the AutoComplete
...
class SomeEntityAutoCompleteType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array options){
$builder->add('some_entity', 'entity_autocomplete', array(...));
}
}
//the controller
public function someAction(){
$form = $this->get('form.factory')->create(new SomeEntityAutoCompleteType());
...
//I want the below line to return my entity
$form->getData();
//but I have to use this one right now
$form['some_entity']->getData()
}
注:我并没有实际测试的另一种方法,但是从我了解的Symfony表单组件的应该是我所描述的方式;
我想我必须使用DataTransformer – user2268997