拖车注册表格 FosUserBundle Symfony

Tow Registration form FosUserBundle Symfony

我使用 symfony 2.8 和 FOSUserBundle,我在同一个 table 数据库中有两种类型的用户,我喜欢在同一注册页面中区分注册表格,如下所示: *

无法在同一页面使用两个表单实例的问题,请问我该怎么办?

我要解决这个问题的方法是覆盖 FOSUserBundle,然后扩展 RegistrationController and likely 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(),
    ]
);

if 条件内的部分可能看起来与原始控制器相似。对于每种用户类型,您可能有不同的 UserManager,您必须切换出去,但除此之外,它基本上是:调度预事件、保存用户、调度 post-事件、重定向。发送这两个事件很重要,因为 FOSUserBundle 的其他部分将依赖它们,例如发送注册邮件。

在您的模板中,您只需在它们的选项卡中呈现这两个表单。您可能需要 fiddle 稍微处理一下表单 ID,但这应该很简单。