symfony3 - 如何在createform中选择对象类型?

时间:2016-08-24 13:06:02

标签: symfony symfony-forms

这是一个非常标准的代码来处理表单

$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上使用另外两种名为AccountHSTypeAccountHGType的类型进行了一些继承,这只会添加一个新字段attributHSattributHG

我想要做的是根据提交者参数在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

0 个答案:

没有答案