2016-10-03 57 views
1

我試圖將一個城市添加到擴展BaseUser從FOSUserBundle的User類中,並且我遵循本頁中的指南: http://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data

我做了一個RegistrationType類,用下面的代碼:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    /* i setted a name field just to check that this form builder is used then i try to create a user */ 
    $builder->add('name'); 
    $builder->add('provincia', EntityType::class, array(
      'class'   => 'AppBundle\Entity\State', 
      'placeholder' => '', # 
     )); 

    $builder->addEventListener(
     FormEvents::PRE_SET_DATA, 
     function (FormEvent $event) { 
      $form = $event->getForm(); 

      $data = $event->getData(); 

      $state = $data->getState(); 
      $cities = null === $state ? array() : $state->getCities(); 

      $form->add('city', EntityType::class, array(
       'class'  => 'AppBundle\Entity\City', 
       'placeholder' => '', 
       'choices'  => $cities, 
      )); 
     } 
    ); 
//... 
} 

的事情是,當它拋出我在$數據 - >的getState()一個錯誤,它告訴我「錯誤:呼叫到一個成員函數getState()null「。 會發生什麼?

+0

您能向我們展示如何在您的控制器中聲明您的表單嗎? –

+0

嗨拉斐爾,我想我沒有聲明一個表格,因爲我從fosuserbundle文檔(http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_forms.html)中瞭解到,我所要做的就是創建一個從包的RegistrationFormType繼承的類,聲明: public function getParent() { return'FOS \ UserBundle \ Form \ Type \ RegistrationFormType'; } 我的整個班級將是: (請看下面的回答,我不能把所有的代碼放在這裏,因爲字符限制在這個迴應中) –

回答

0
class RegistrationType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('name'); 
     $builder->add('state', EntityType::class, array(
      'class'   => 'AppBundle\Entity\State', 
      'mapped'  => false, 
      'placeholder' => '', # 
     )); 

     $formModifier = function (FormInterface $form, State $state= null) { 
      $cities = null === $state? array() : $state->getCities(); 

      $form->add('city', EntityType::class, array(
       'class'  => 'AppBundle\Entity\City', 
       'placeholder' => '-Choose a state-', 
       'choices'  => $cities, 
      )); 
     }; 

     $builder->addEventListener(
      FormEvents::PRE_SET_DATA, 
      function (FormEvent $event) use ($formModifier) { 
       $data = $event->getData(); 

       if($data === null || !method_exists($data, 'getState')) { 
        $formModifier($event->getForm(), null); 
       } else { 
        $formModifier($event->getForm(), $data->getProvincia()); 

       } 
      } 
     ); 

     $builder->get('state')->addEventListener(
      FormEvents::POST_SUBMIT, 
      function (FormEvent $event) use ($formModifier) { 
       // It's important here to fetch $event->getForm()->getData(), as 
       // $event->getData() will get you the client data (that is, the ID) 
       $state = $event->getForm()->getData(); 

       // since we've added the listener to the child, we'll have to pass on 
       // the parent to the callback functions! 
       $formModifier($event->getForm()->getParent(), $provincia); 
      } 
     ); 
    } 

    public function getParent() 
    { 
     return 'FOS\UserBundle\Form\Type\RegistrationFormType'; 
    } 

    public function getBlockPrefix() 
    { 
     return 'app_user_registration'; 
    } 

    public function getName() 
    { 
     return $this->getBlockPrefix(); 
    } 
} 

真正驅使我瘋狂的是,$事件 - >的getData(),應根據官方文檔例如帶的東西,在我的情況來空的事實。我設法解決了這個註冊表格的問題,但現在我的個人資料編輯表單出現了同樣的問題argggghhhhh