How to redefine the registration form of FosUser and the choice of fields Fields with roles SYMFONY2

I followed the documentation to redefine the registration form of FosUser, and I show the roles that I want, like this. Here is my registration 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 identity.

 <?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 problem now is that I don’t know how to display in this field only the roles that I added, because I get ROLE_USER with selections 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 Sorry, I could not add other tags: P

+4
source share
2 answers

I think the problem is that you are using ChoiceField . ChoiceField returns only one role (row type, this role identifier), but the setRoles method expects an array. This means that you need to either add the multiple => true option, or switch to another type of field, for example, in the Collection field. Using multiple will return the array that setRoles will setRoles , and using the Collection field will also return the array.

In the bottom line, you need to select a form field that returns an array instead of a single result, a string. You can view all types of forms here.

Hope this helps.

+4
source

I also have the same problem, so I use this line of code in the 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 the controller

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

Source: https://habr.com/ru/post/1382081/


All Articles