Symfony2:在 FormBuilder 中获取用户角色列表 [英] Symfony2: Getting the list of user roles in FormBuilder
问题描述
我正在制作一个用于创建用户的表单,我想在创建用户时为其分配一个或多个角色.
I'm making a form for user creation, and I want to give one or several roles to a user when I create him.
如何获取security.yml
中定义的角色列表?
How do I get the list of roles defined in security.yml
?
这是我目前的表单构建器:
Here's my form builder at the moment:
public function buildForm(FormBuilder $builder, array $options)
{
parent::buildForm($builder, $options);
// add your custom fields
$user = new User();
$builder->add('regionUser');
$builder->add('roles' ,'choice' ,array('choices' => $user->getRolesNames(),
'required' => true,
));
}
并在 User.php 中
and in User.php
public function getRolesNames(){
return array(
"ADMIN" => "Administrateur",
"ANIMATOR" => "Animateur",
"USER" => "Utilisateur",
);
}
当然,这个方案是行不通的,因为roles
在数据库中被定义为位图,所以无法创建choices
列表.
Of course, this solution doesn't work, because roles
is defined as a bitmap in the database, therefore the choices
list cannot be created.
提前致谢.
推荐答案
security.role_hierarchy.roles
容器参数将角色层次结构保存为数组.您可以对其进行概括以获取定义的角色列表.
security.role_hierarchy.roles
container parameter holds the role hierarchy as an array. You can generalize it to get list of roles defined.
这篇关于Symfony2:在 FormBuilder 中获取用户角色列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!