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「。 會發生什麼?
您能向我們展示如何在您的控制器中聲明您的表單嗎? –
嗨拉斐爾,我想我沒有聲明一個表格,因爲我從fosuserbundle文檔(http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_forms.html)中瞭解到,我所要做的就是創建一個從包的RegistrationFormType繼承的類,聲明: public function getParent() { return'FOS \ UserBundle \ Form \ Type \ RegistrationFormType'; } 我的整個班級將是: (請看下面的回答,我不能把所有的代碼放在這裏,因爲字符限制在這個迴應中) –