Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:20:37+00:00 2026-06-03T17:20:37+00:00

I have a user edit form where I would like to administer the roles

  • 0

I have a user edit form where I would like to administer the roles assigned to a user.

Currently I have a multi-select list, but I have no way of populating it with the role hierarchy defined in security.yml.

Is there some way that I get this information to the form builder in the FormType class?

$builder->add('roles', 'choice', array(
                'required' => true,
                'multiple' => true,
                'choices' => array(),
            ));

Looking around I found that I can get the roles from the container in a controller with:

$roles = $this->container->getParameter('security.role_hierarchy.roles');

I have also discovered that I could potentially set this as a dependency to be injected on the FormType class in services.xml:

<parameters>
    <parameter key="security.role_heirarchy.roles">ROLE_GUEST</parameter>
</parameters>
<services>
    <service id="base.user.form.type.user_form" class="Base\UserBundle\Form\UserType" public="false">
        <tag name="form.type" />
        <call method="setRoles">
            <argument>%security.role_heirarchy.roles%</argument>
        </call>
    </service>
</services>

This however does not work and does not seem to ever call the setRoles method.

So how can I get this to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:20:38+00:00Added an answer on June 3, 2026 at 5:20 pm

    In your controller

    $editForm = $this->createForm(new UserType(), $entity, array('roles' => $this->container->getParameter('security.role_hierarchy.roles')));
    

    In UserType :

    $builder->add('roles', 'choice', array(
        'required' => true,
        'multiple' => true,
        'choices' => $this->refactorRoles($options['roles'])
    ))
    
    [...]
    
    public function getDefaultOptions()
    {
        return array(
            'roles' => null
        );
    }
    
    private function refactorRoles($originRoles)
    {
        $roles = array();
        $rolesAdded = array();
    
        // Add herited roles
        foreach ($originRoles as $roleParent => $rolesHerit) {
            $tmpRoles = array_values($rolesHerit);
            $rolesAdded = array_merge($rolesAdded, $tmpRoles);
            $roles[$roleParent] = array_combine($tmpRoles, $tmpRoles);
        }
        // Add missing superparent roles
        $rolesParent = array_keys($originRoles);
        foreach ($rolesParent as $roleParent) {
            if (!in_array($roleParent, $rolesAdded)) {
                $roles['-----'][$roleParent] = $roleParent;
            }
        }
    
        return $roles;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an User model who has_one :preference , but I would like to
I have a simple form where a user can add, edit, and delete people
I have a nested form in which I would like the form fields to
I have a form that allows a user to update their profile information, but
When editing a basic User model in Rails from the edit.html.erb form, I would
I am new to Rails. I have a User model. I would like a
I have a form that a user would fill out while creating a ticket
I have a form where a user can select a rating from 1-5. Each
I have a user model with a settings page at /users/1/edit Currently you can
i have a Zend Form and i would like to use jquery to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.