Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: How to build multiple registration forms with FOSUserBundle

I'm absolutely new to Symfony2 but already in love with this framework.

I'm having a project that requires to have 3 different user types.

One will be super admin, other two will be a regular users but with different profile layouts. Different layouts for profile means registration form for those two user types must have different form fields and different layouts in the application after registration.

I think I've got the idea (don't know if that will be OK later) how to manage users after registration. But for now I don't understand how to build two different registration forms that will be extended from FOSUserBundle.

As far as I understand FOSUB have only one point in the configuration where registration is set up and cannot have multiple "registration:"

In the config.yml I have:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Company\UserBundle\Entity\User
    registration:
        confirmation: { enabled: true }

security.yml:

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    main:
        pattern:  ^/
        form_login:
            provider: fos_userbundle
            csrf_provider:  form.csrf_provider
        logout:       true
        anonymous:    true

My idea is to create three different bundles that extends FOSUB, but documentation says that only one bundle can declare FOSUB as parent.

Another option (from documentation) is to extend ContainerAware implementing all original methods. But even in this case, I don't get it how I have to configure app/config/{config|security|routing}.yml to have for instance /register_employee, /register_manager links that will use FOSUB and my custom forms?

Please point me to right direction. Thanks.

like image 548
Juokas Juokelis Avatar asked May 04 '12 15:05

Juokas Juokelis


3 Answers

Yes it can be doable. But it will be a complex procedure. You can do following (I havn't tried myself though :) )

  • Create a route pattern like /register/{type} and modify registerAction accordingly. Take a look at this doc for overriding controllers.

  • In registerAction method create a blank User entity and set the type from the route argument. Then call $form->setData(), e.g [1]

  • Override the RegistrationFormType to add an event listener that will listen on FormEvents::PRE_SET_DATA data event. Here you will add form fields dynamically based on type. Check this cookbook entry.

  • Now comes the validation part. Create two types of validation groups for each user type. You can dynamically set validation group based on submitted data. Check here.

  • If you want to do extra operation during form save you can override onSuccess method of RegistrationFormHandler. Check at bottom of this doc to do that.

[1] like this,

public function registerAction()
{
    $form = $this->container->get('fos_user.registration.form');
    $user = new User();
    $form->setData($user);
    //.....
}

Edit:

Third step will not work. You have to override process method of RegistrationFormHandler to pass the type argument from the controller action.

like image 191
Mun Mun Das Avatar answered Oct 13 '22 03:10

Mun Mun Das


FOSUserBundle is not to provide authentication of users but to manage users. The bundle provides you a better structure to manage users for your application. If you take a look at the code you will find controllers that are used for the default registration and other user activities.

Ideally you would want to write your own controller and make use of the bundle to only store and retrieve users. This is especially required when you want to manage multiple registration/login forms.

Things would seem clearer once you start to consider FOSUserBundle only to manage users and not to authenticate/register them.

You might also want to look at https://github.com/sonata-project/SonataUserBundle, which provides integration between SonataAdminBundle and FOSUserBundle.

like image 25
arghav Avatar answered Oct 13 '22 05:10

arghav


check this bundle https://github.com/PUGX/PUGXMultiUserBundle. It offers all staff to work with multiple types of users, including registration

like image 26
nucleartux Avatar answered Oct 13 '22 03:10

nucleartux