0
votes

I work with symfony 2.8 and FOSUserBundle, I have two type of user in the same table in database , And I like to differenciate the registration form in the same page of registration like this : img*

the problem that I can't use two instance of the form in the same page, what can I do please?

1
Why can you not use 2 instances of the same form in the page? You just have to pass both forms to the template and then on submit check which form you have to handle. Can you please elaborate what's your exact issue when doing this approach? - dbrumann
Because I use FosUserBundle and I did not overried its controller, I just overied the 'RegisterFormType', In fact if I use form_widget, for the first form works well, but for the second form the inputes does not appear, it just displays the labs - Dhia Eddine Farah
Ah, ok that makes sense. FOSUserBundle places quite a few constraints on how to deal with signup/signin. Any chance to have registration not be handled by FOSUserBundle or extend the Bundle and overwrite the RegistrationController? - dbrumann
I want to go through FOSUserBundle, and if there was a solution by overloading the controller RegisterController it is not a problem I want any solution to solved this problem because I had three days without solution - Dhia Eddine Farah
Well in that case I would extend the default controller and template like described in the documentation: symfony.com/doc/current/bundles/FOSUserBundle/… and symfony.com/doc/current/bundles/FOSUserBundle/… respectively - dbrumann

1 Answers

2
votes

The way I would go about this, is override FOSUserBundle and then extend the RegistrationController and likely the corresponding template.

In the registerAction you can reuse some parts of the original, but where the form is created you then create two different ones, maybe like this:

/** @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(),
    ]
);

The part inside the if conditions will then probably look similar as to the original controller. You might have different UserManager's for each user type, you have to switch out, but other than that it's basically: dispatch pre-event, save user, dispatch post-event, redirect. It is important that you dispatch both events as other parts of FOSUserBundle will rely on them, e.g. sending a registration email.

In your template you then just render both forms in their tab. You might have to fiddle around with the form id's a bit, but that should be straightforward.