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 9108853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:54:10+00:00 2026-06-17T02:54:10+00:00

I am having a problem creating a new/edit user form in symfony 2 +

  • 0

I am having a problem creating a new/edit user form in symfony 2 + FOSUserBundle. I would like to enable an admin to select the user role from a dropdown or radio button list but I can’t seem to get it to work. I found this answer here How can I pass a full security roles list/hierarchy to a FormType class in Symfony2? which is the most relevent thing that I could find but it doesn’t work.

This is the UserType form as it currently stands. I would like to get the roles from the container but I can’t seem to get that to work either without it throwing an error. The roles will populate the dropdown properly but it will not show which role is currently assigned and it won’t allow for the information to be updated because it expected it to be an array $entity->addRoles(array('ROLE_SUPER_ADMIN'));, but it just submits as a string.

namespace Wes\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
    private $roles;

    public function __construct($roles) {
        $this->roles = $roles;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username')
            ->add('email')
            ->add('enabled')
            ->add('roles', 'choice', array(
               'choices' => $this->flattenArray($this->roles),
            ))
            ->add('firstName')
            ->add('lastName')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Wes\AdminBundle\Entity\User',
            'roles' => null,
            'userRole' => null,
        ));
    }

    public function getName()
    {
        return 'wes_adminbundle_usertype';
    }

    private function flattenArray(array $data)
    {
        $returnData = array();

        foreach($data as $key => $value)
        {
            $tempValue = str_replace("ROLE_", '', $key);
            $tempValue = ucwords(strtolower(str_replace("_", ' ', $tempValue)));
            $returnData[$key] = $tempValue;
        }
        return $returnData;
    }
}

This is the controller.

public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('WesAdminBundle:User')->find($id);
    if (!$entity) {
        throw $this->createNotFoundException('Unable to find User entity.');
    }

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

    return $this->render('WesAdminBundle:User:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
 * Edits an existing User entity.
 *
 */
public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('WesAdminBundle:User')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find User entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createForm(new UserType($this->container->getParameter('security.role_hierarchy.roles')), $entity);
    $editForm->bind($request);

    if ($editForm->isValid()) {
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('wes_admin_user_edit', array('id' => $id)));
    }

    return $this->render('WesAdminBundle:User:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

I have been fighting this for a few days now and I can’t seem to get it to work properly. Any thoughts?

  • 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-17T02:54:11+00:00Added an answer on June 17, 2026 at 2:54 am

    Use the userManager provided by FOSUserBundle instead of custom persistence method, because roles array need to be serialized before it is stored to database.

    $userManager = $this->container->get('fos_user.user_manager');
    $user = $userManager->findUserBy(array('id' => $id));
    
    $editForm = $this->createForm(new UserType($this->container->getParameter('security.role_hierarchy.roles')), $user);
    
    if ($editForm->isValid()) {
        $userManager->updateUser($user);
    
        return $this->redirect($this->generateUrl('wes_admin_user_edit',
            array('id' => $id)));
    }
    

    See https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/user_manager.rst (edited) for more info.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with Symfony creating a new session on each page load,
I am having a problem creating a WCF service instance with a parameter. The
I've been having a problem creating a checklist in the style of the TouchCells
i am having problem in creating a video player in flash via as3, the
I'm having a problem creating a trigger on a database for a project I've
I am having a problem with creating a navigation controller after on the other
Having a bit of a problem creating an instance of an object. Bear in
I am creating an menu and having problem to access the element to hide
I'm creating shopping cart and I'm having one problem: When someone orders an item
I've got a Controller ActionResult that looks like this Function Edit(ByVal user As Domain.User,

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.