Search code examples
phpsymfonyfosuserbundle

FOSUserBundle - Creating new user specifying role


Would like to know how can achieve the creation of a new user but specifying his role. At the moment, when i created a new user the program automatically associated with ROLE_ADMIN role.

Any information/code/example is very welcome.


Solution

  • Ok. I solved my problem. Basically i follow this guide, and changed some aspects.

    1. On my model, i added the field for roles: protected $roles = array();
    2. Changed my Form/Type/RegistrationFormType.php and put the following control:

    Code:

    $user = new UserAdmin();
        $builder->add('roles', 'collection', array(
            'type'   => 'choice',
            'options'  => array(
                'choices'  => array(
                    'nashville' => 'Nashville',
                    'paris'     => 'Paris',
                    'berlin'    => 'Berlin',
                    'london'    => 'London',
                ),
            ),
    ));
    

    Now when i opened my register view it shows a dropdownlist. In this case the options are: Nashville, Paris, Berlin, London.