4
votes

I followed the documentation to override the register form of the FosUser and I display the roles I want like this. Here is my register form.

<?php

namespace My\BlogBundle\Form;
use My\BlogBundle\Entity\User; 
use Symfony\Component\Form\FormBuilder;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;

class MyRegisterType extends BaseType
{
 public function buildForm(FormBuilder $builder, array $options)
 {
    parent::buildForm($builder ,$options);
    $user = new User();
    $builder
        ->add('roles' ,'choice' ,array('choices'=>$user->getRoles() ) ;

 }

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

And here is my User entity.

<?php

namespace My\BlogBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
* My\BlogBundle\Entity\User
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="My\BlogBundle\Entity\UserRepository")
*/
class User  extends BaseUser
{
 /**
  * @var integer $id
  * 
  * @ORM\Column(name="id", type="integer")
  * @ORM\Id
  * @ORM\GeneratedValue(strategy="AUTO")
  */
 protected $id;

 protected $roles=array();




/**
 *@ORM\OneToMany(targetEntity="Article" ,mappedBy="user")
 */
protected $article;

/**
 *@ORM\OneToMany(targetEntity="Comment" ,mappedBy="user")
 */
protected $comment;

 public function __construct()
 {
    parent::__construct();
    $this->roles=array('searcher' ,'annoucer');
 }

}

My issue now is I don't know how to display on that field only the roles I added because I get ROLE_USER with the choices also and when I submit the form I get this error

Catchable Fatal Error: Argument 1 passed to FOS\UserBundle\Model\User::setRoles() must be an array, string given, called in /var/www/blog/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 346 and defined in /var/www/blog/vendor/bundles/FOS/UserBundle/Model/User.php line 709

Any help would be more than appreciated, thanks. BTW I'm sorry I couldn't add other tags :P

2

2 Answers

4
votes

I think you problem is because you are using a ChoiceField. A ChoiceField will return only one role (a string type, this id of the role) but the method setRoles expect an array. This means you need to either add the option multiple => true or change to another type of field like a Collection field. Using multiple will return an array which will be accepted by setRoles and using a Collection field will also return an array.

Bottom line, you need to choose a form field that returns an array instead of a single result, a string. You can see all form types here

Hope this helps.

1
votes

I also have the same issue, then i use this line of code in controller to resolve it.

in your registration form

->add('roles', 'choice', array(
                'mapped' => false,
                'required' => true,
                'label'    => 'User Type',
                'choices' => array(
                    'ROLE_USER' => 'User',
                    'ROLE_STAFF' => 'Staff',
                    'ROLE_INSTITUTE' => 'Institute',
                ),
                'expanded'   => true,
            ))

and in controller

    $role = $form->get('roles')->getData();
    $user->setRoles(array($role));
    $em->persist($user);
    $em->flush();