I\'m making a form for user creation, and I want to give one or several roles to a user when I create him.
How do I get the list of roles defined in security.y
In Symfony 3.3, you can create a RolesType.php as follows:
roles = array_unique($roles);
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'choices' => $this->roles,
'attr' => array(
'class' => 'form-control',
'aria-hidden' => 'true',
'ref' => 'input',
'multiple' => '',
'tabindex' => '-1'
),
'required' => true,
'multiple' => true,
'empty_data' => null,
'label_attr' => array(
'class' => 'control-label'
)
));
}
public function getParent() {
return ChoiceType::class;
}
}
Then add it to the form as follows:
$builder->add('roles', RolesType::class,array(
'label' => 'Roles'
));
Important is that each role must also be contained, for example: ROLE_ADMIN: [ROLE_ADMIN, ROLE_USER]