这是一个非常标准的代码来处理表单
$account = new Account();
$form = $this->createForm('TEST\RegistrationBundle\Form\AccountType', $account);
$form->handleRequest($request);
if($form->isValid())
{
$this->getDoctrine()->getManager()->persist($account);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('test_registration_homepage');
}
return $this->render('TESTRegistrationBundle:Default:index.html.twig', array('form_account' => $form->createView()));
我还在TEST\RegistrationBundle\Form\AccountType
上使用另外两种名为AccountHSType
和AccountHGType
的类型进行了一些继承,这只会添加一个新字段attributHS
或attributHG
。
我想要做的是根据提交者参数在createFrom()
中使用正确的类型,所以:
$form = $this->createForm('TEST\RegistrationBundle\Form\AccountHSType', $account);
或
$form = $this->createForm('TEST\RegistrationBundle\Form\AccountHGType', $account);
或
$form = $this->createForm('TEST\RegistrationBundle\Form\AccountType', $account);
用户选择所需类型,并随表单一起提交以及其他属性。选择正确类型的简单方法是使用:
$type_id = $request->query->get('city_id');
但是我想知道是否有一种更聪明的方法可以做到这一点,例如更早地给对象$Account
加水。
希望我足够清楚。
提前致谢
d