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