【发布时间】:2015-02-27 07:07:02
【问题描述】:
我创建了一个为实体添加自动完成功能的表单类型,但是它需要为每个实体进行一些配置,即:我必须将配置传递给选项数组,所以我决定为每个实体创建一个新的 FormType使用我创建的 AutoCompleteType 并重用它们。但是我希望这些 Formtypes 即:每个特定实体的那些,在调用 getData() 时返回实体,现在发生的是我必须首先检索 ParentForm 的字段包含AutoCompleteType 然后调用getData() 来检索我的实体。如何将这些信息直接映射到ParentForm 上?
//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