2013-06-27 49 views
28

我試圖讓自己的表單中的實體。我懶得記得一個getData()方法,但它不存在於表單內,我不記得如何使用它。如何訪問表單中的buildForm方法內部的實體

有沒有什麼辦法讓實體在buildform方法中不使用事件偵聽器?

我的意思是,像這樣:

public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     /*some method to get the entity of the form such as getEntity????????*/ 
     $builder->add('field'); 
    } 

回答

54

我發現:

public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $entity = $builder->getData(); 
     $builder->add('field'); 
    } 
+22

請注意,如果你是在收集使用類型,你需要使用表單事件 $ builder- > addEventListener(FormEvents :: PRE_SET_DATA,function(FormEvent $ event)use($ builder){ /** @var YourEntity $ data */ $ data = $事件 - >的getData(); }); – nvvetal

相關問題