Trying to create a form that will allow me to select User roles but it's not working.
Here is my User class:
namespace TestBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="TestBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
}
And here is my form definition:
class ChangeUserRolesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$permissions = array(
'ROLE_USER' => 'First role',
'ROLE_CONSULT' => 'Second role',
'ROLE_SUPER_ADMIN' => 'Third role'
);
$builder
->add('username', EntityType::class, array(
'class' => 'TestBundle:User',
'choice_label' => 'username'
))
->add(
'roles',
ChoiceType::class,
array(
'label' => 'Roles',
'choices' => $permissions,
'multiple' => true,
'expanded' => true
)
)
->add(
'save',
SubmitType::class
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TestBundle\Entity\User',
));
}
}
This is my controller action that creates the form:
/**
* @Route("/")
*/
public function indexAction()
{
$users = $this->getDoctrine()->getRepository('TestBundle:User')->findAll();
$user = $this->getDoctrine()->getRepository('TestBundle:User')->findOneBy(array('username' => 'liviu'));
$editUserForm = $this->createForm('TestBundle\Form\ChangeUserRolesType', $user);
return $this->render('TestBundle:Default:index.html.twig', array(
'users' => $users,
'form' => $editUserForm->createView()
));
}
But I am getting this error:
Warning: Missing argument 1 for FOS\UserBundle\Model\User::hasRole(), called in /home/liviu/apps/app1/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 416 and defined 500 Internal Server Error - ContextErrorException
does anyone know what's going on ? I am on Symfony 3.0.*