I am creating a user form in which Roles are selected via form dropdown.My form and entity looks like this
//entity
private $roles;
public function getRoles()
{
return $this->roles;
}
//form
class RoleType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choices' => array(
'ROLE_USER' => 'ROLE_USER',
'ROLE_ADMIN' => 'ROLE_ADMIN',
)
));
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'role';
}
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('username')
->add('password', 'repeated', array(
'type' => 'password',
'invalid_message' => 'The password fields must match.',
'options' => array('attr' => array('class' => 'password-field')),
'required' => true,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
))
//->add('terms', 'checkbox', array(
// 'property_path' => 'termsAccepted',
// ))
->add('firstname')
->add('lastname')
->remove('photo')
->add('email', 'email')
->remove('status')
->remove('createdBy')
->remove('isActive')
->remove('dateCreated')
->remove('updatedBy')
->remove('dateUpdated')
//->remove('roles', 'choice', array(
// 'choices' => array('ROLE_USER' => 'ROLE_USER', 'ROLE_ADMIN' => 'ROLE_ADMIN'),
//))
->add('roles', new RoleType(), array(
'placeholder' => 'Choose Roles',
))
//->add('terms', 'checkbox', array(
// 'property_path' => 'termsAccepted',
// 'attr' => array('class' => 'checkbox'),
// ))
->add('Register', 'submit')
;
}
public function getName()
{
return 'registration';
}
I have no problem when creating a user where Roles are selected via form dropdown.The problem is, during login, it throws errors that something must be in array, not in string
Catchable Fatal Error: Argument 4 passed to Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct() must be of the type array, string given, called in /var/www/
I change my user entity
private $roles = array();
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
public function getRoles()
{
return array($this->roles);
}
The user can now successfully login, but it throws an error when adding another user in the form
The value of type "array" cannot be converted to a valid array key.
This is just a simple user bundle.I dont want to use another third party bundle like FOSUsersrBundle.Any idea how to fix this?
update
In the controller, I use to store this into database
public function createAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
//$form = $this->createForm(new RegistrationType(), new Registration());
$form = $this->createForm(new RegistrationType(), new Users());
$form->handleRequest($request);
if ($form->isValid()) {
$registration = new Users();
$registration = $form->getData();
$registration->setDateCreated(new \DateTime());
//$registration->setRoles('ROLE_ADMIN');
$registration->setPhoto('http://www.gravatar.com/avatar/'.md5(trim($request->get('email'))));
$pwd=$registration->getPassword();
$encoder=$this->container->get('security.password_encoder');
$pwd=$encoder->encodePassword($registration, $pwd);
$registration->setPassword($pwd);
$em->persist($registration);
$em->flush();
$this->addFlash('danger', 'New User successfully added!');
return $this->redirect($this->generateUrl('voters_list'));
} else {
$this->addFlash('danger', 'Some errors buddy cannot proceed, please check!');
return $this->render('DuterteBundle:Security:register.html.twig',array(
'form' => $form->createView())
);
}
update
My basic mapping
Project\Bundle\DuterteBundle\Entity\Users:
type: entity
table: users
repositoryClass: Project\Bundle\DuterteBundle\Repository\UsersRepository
#indexes:
#role_id_idx:
#columns:
#- role_id
id:
id:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
generator:
strategy: IDENTITY
fields:
username:
type: string
nullable: false
length: 50
fixed: false
comment: ''
password:
type: string
nullable: false
length: 32
fixed: false
comment: ''
firstname:
type: string
nullable: true
length: 50
fixed: false
comment: ''
lastname:
type: string
nullable: true
length: 50
fixed: false
comment: ''
photo:
type: string
nullable: true
length: 200
fixed: false
comment: ''
email:
type: string
nullable: true
length: 200
fixed: false
comment: ''
status:
type: string
nullable: true
length: 8
fixed: false
comment: ''
roles:
type: string
nullable: true
length: 100
fixed: false
comment: ''
in mysql, the users table includes the roles column where the roles are stored e.g 'ROLE_USERS',"ROLE_ADMIN' etc.Changing the getRoles function from the users entity
public function getRoles()
{
return array('ROLE_USER'_)
}
Will successfully logs a user, but the roles is always set to 'ROLE_USERS' even if int the database, a user's role is equivalent to 'ROLE_ADMIN'