将角色字段添加到FOSUserBundle组表单 [英] Add roles field to FOSUserBundle group form

查看:50
本文介绍了将角色字段添加到FOSUserBundle组表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

激活组时(根据其文档在FOSUserBundle中,组角色未嵌入在编辑和更新表单中!我已经覆盖了GroupFormType和GroupController,但是无法将角色从控制器传递给表单类. 我的问题是如何添加角色到表单中,以允许管理员更改角色或将角色分配给组?

When activating group( base on its documentation) in FOSUserBundle, the group roles are not embedded on edit and update form!I already override GroupFormType and GroupController but I can't pass roles from controller to form class. my question is how can I add roles to form to let administrator change or assign role to groups?

推荐答案

通过添加角色字段来覆盖GroupController来解决我的问题

Solving my problem by adding the role field to override GroupController

public function editAction(Request $request, $groupName)
{
   ...

    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
    $formFactory = $this->get('fos_user.group.form.factory');

    $form = $formFactory->createForm();
    $form->add('roles', 'choice', array(
        'choices' => $this->getExistingRoles(),
        'data' => $group->getRoles(),
        'label' => 'Roles',
        'expanded' => true,
        'multiple' => true,
        'mapped' => true,
    ));

  ...
}
public function getExistingRoles()
{
    $roleHierarchy = $this->container->getParameter('security.role_hierarchy.roles');
    $roles = array_keys($roleHierarchy);

    foreach ($roles as $role) {
        $theRoles[$role] = $role;
    }
    return $theRoles;
}

这篇关于将角色字段添加到FOSUserBundle组表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆