2017-08-01 96 views
0

我在symfony 2.8和FOSUserBundle工作,我有两个类型的用户在数据库中的同一个表,而且我喜欢者区分登记表中登记的同一个页面是这样的: img *拖车登记表FosUserBundle Symfony的

我不能在同一页面中使用表格的两个实例的问题,我该怎么办?

+0

为什么不能在页面中使用2个相同表单的实例?您只需将两个表单都传递给模板,然后在提交时检查您必须处理的表单。你能否详细说明在做这种方法时你的确切问题是什么? – dbrumann

+0

因为我使用FosUserBundle,并且我没有过度使用它的控制器,所以我只是覆盖了'RegisterFormType', 事实上,如果我使用form_widget,对于第一种形式效果很好,但对于第二种形式,不会出现inputes,它只显示实验室 –

+0

啊,好吧,这是有道理的。 FOSUserBundle对于如何处理注册/登录有很多限制。是否有机会注册不由FOSUserBundle处理或扩展Bundle并覆盖RegistrationController? – dbrumann

回答

2

我会这样做的方式是重写FOSUserBundle,然后再扩大RegistrationController和可能的the corresponding template

registerAction可以重用的the original某些部分,但在创建表单,您创建两个不同的人,也许是这样的:

/** @var $formFactory FactoryInterface */ 
$clientFormFactory = $this->get('client_registration.form.factory'); 
$clientForm = $clientFormFactory->createForm(); 
$clientForm->setData($client); 

/** @var $formFactory FactoryInterface */ 
$correspondentFormFactory = $this->get('correspondent_registration.form.factory'); 
$correspondentForm = $correspondentFormFactory->createForm(); 
$correspondentForm->setData($correspondent); 

$clientForm->handleRequest($request); 
$correspondentForm->handleRequest($request); 

if ($clientForm->isSubmitted() && $clientForm->isValid()) { 
    // ... 
} elseif ($correspondentForm->isSubmitted() && $correspondentForm->isValid()) { 
    // ... 
} 

return $this->render(
    '@FOSUser/Registration/register.html.twig', 
    [ 
     'clientForm' => $clientForm->createView(), 
     'correspondentForm' => $correspondentForm->createView(), 
    ] 
); 

内如果条件那么很可能类似于该部分至于原来的控制器。对于每种用户类型,您可能会有不同的UserManager,但除此之外它基本上是:派发事件前,保存用户,派发事件,重定向。因为FOSUserBundle的其他部分将依赖它们,所以您必须派遣这两个事件,例如,发送注册电子邮件。

在您的模板中,您只需在其选项卡中呈现这两个表单。你可能不得不摆弄一下表单id,但这应该很简单。

+0

非常感谢dbrumann它工作正常:) –