2014-12-31 61 views
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表單組件的應該是我所描述的方式;

+0

我想我必須使用DataTransformer – user2268997

回答

0

我通過將我的SomeEntityAutoCompleteType的父類型設置爲我創建的主自動完成類型並使用setDefaultOptions()方法配置選項來解決此問題。

//SomeEntityAutoCompleteType 
public function setDefaultOption(OptionsResolverInterface $resolver){ 
    $resolver->setDefaults(...); 
} 

public function getParent(){ 
    return "autocomplete_type";//this is the main autocomplete type I mentioned 
} 
相關問題