3

I would like to create a form to edit my users. Users and roles connected with ManyToMany. In UserUsers entity I have a $roles variable which is ArrayCollection:

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

On my form I would like to add roles to my users via multiple select form element. In my user form:

public function buildForm( FormBuilderInterface $builder, array $options ) {
    $builder->add( 'username' )
            ->add( 'password', 'repeated', array( 
                    'type' => 'password',
                    'mapped' => false,
                    'required' => false,
                    'first_options' => array( 
                            'label' => 'Password' ),
                    'second_options' => array( 
                            'label' => 'Repeat Password' ) ) )
            ->add( 'roles', 'choice', array( 
                    'mapped' => false,
                    'multiple' => true ) );
}

Now my multiple select is empty.

If I turn mapped to true, I got an error message:

UserRoles could not be converted to int in...

I've tried a lots of ways, but I could not solve this problem correctly.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
lordjancso
  • 135
  • 2
  • 2
  • 10
  • What type do you have in your $roles ArrayCollection? – redbirdo May 09 '13 at 13:11
  • All elements in $roles ArrayCollection is an entity (UserRoles). – lordjancso May 09 '13 at 14:26
  • For a choice of entities you should use the special choice field type 'entity' (http://symfony.com/doc/current/reference/forms/types/entity.html). For an example see my answer to a similar question - http://stackoverflow.com/questions/13519961/symfony-2-form-create-user-and-add-group/13521462#13521462 – redbirdo May 09 '13 at 15:50
  • Okay, I changed roles like this: `$builder->add( 'roles', 'entity', array( 'class' => 'AcmeUserBundle:UserRoles', 'multiple' => true, 'property' => 'name' ) );` Then I got an error message: Expected argument of type **"Doctrine\Common\Collections\Collection", "array" given** – lordjancso May 10 '13 at 07:35
  • That's odd, as ArrayCollection implements Doctrine\Common\Collections\Collection. Is it possible that roles() is reassigned to an array elsewhere? Or that ArrayCollection is not resolving to the doctrine class? – redbirdo May 10 '13 at 08:14
  • I fould the problem. In UserUsers entity I have this: ` public function getRoles() { return $this->roles ->toArray(); }` Here I convert the roles to array, because in my loginController I have to pass the roles as array: `$token = new UsernamePasswordToken( $user, $user->getPassword(), 'admin_area', $user->getRoles());` If I remove the toArray() method in UserUsers getRoles getter, and modify loginController like this: `$token = new UsernamePasswordToken( $user, $user->getPassword(), 'admin_area', $user->getRoles()->toArray());` Then I have an error message everywhere: – lordjancso May 10 '13 at 08:24
  • Catchable Fatal Error: Argument 4 passed to Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct() must be of the type array, object given, called in C:\wamp\www\admin\symfony\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider.php on line 95 and defined in C:\wamp\www\admin\symfony\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken.php line 34 – lordjancso May 10 '13 at 08:24
  • Is this now the same issue as: http://stackoverflow.com/questions/12200507/role-interface-and-manage-roles? – redbirdo May 10 '13 at 08:33
  • Yes, it is and I did just the same in UserUsers entity: ` public function getRoles() { return $this->roles ->toArray(); } public function getRolesAsObject() { return $this->roles; }` Now it is working: `$builder->add( 'rolesAsObject', 'entity', array( 'label' => 'roles', 'class' => 'AcmeUserBundle:UserRoles', 'multiple' => true, 'property' => 'name' ) );` BUT :) As you can see, my property is the name of the role which is a number, because it is a language key id. Can I call a function on this property somehow? – lordjancso May 10 '13 at 08:43
  • This sounds like a separate issue - i.e. is it that you need a translated value for the role name? If so I suggest create another question. – redbirdo May 10 '13 at 08:48
  • okay, i made a new question about this: http://stackoverflow.com/questions/16479052/symfony2-translate-form-entity-property – lordjancso May 10 '13 at 09:26

2 Answers2

6

For a choice of entities you should use the special choice field type 'entity' (see Symfony manual for entity field type). For an example see my answer to a similar question. If you get further errors you may also find this question on Role Interface and Manage Roles helpful.

Community
  • 1
  • 1
redbirdo
  • 4,937
  • 1
  • 30
  • 34
0

for the fosuserbundle I do it like that:

        $builder->add('roles', 'choice', array(
        'multiple' => true,
        'choices' => array(
            'ROLE_USER' => 'User',
            'ROLE_AUTHOR' => 'Autor',
            'ROLE_MODERATOR' => 'Moderator',
            'ROLE_ADMIN' => 'Admin'
        ),
        'label' => 'Rollen',
        'required' => true
    ));
stwe
  • 1,262
  • 15
  • 18
  • This would be good, but I store the roles in the database and when I open the users to edit, I want to be selected those roles which are assigned to the current user. – lordjancso May 09 '13 at 13:54
  • I have already understood and I strongly recommend the use of FOSUserBundle - Because of the security. There you can store multiple roles to a user. This is the end what you want. – stwe May 09 '13 at 14:50