我在Sonata面板中有一个表单,可以将角色设置为一个组
我希望在复选框中使用相同的角色字段,因此我添加了’expanded’=>对我的领域真实
protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('name') ->add('roles',SecurityRolesType::class,[ 'expanded' => true,]) ; }
但我明白了
我正在尝试将此角色ROLE_ADMIN_USER_EDIT,ROLE_ADMIN_USER_LIST …分组到ROLE_ADMIN_USER
我尝试了一些我在这里找到的选项:http://symfony.com/doc/current/reference/forms/types/choice.html但没有任何效果
如何将此角色分组,如我的第一个屏幕截图,但有复选框?
谢谢
解决方法
为此,您需要覆盖Sonata base_edit_form_macro模板并更改默认渲染
{# If field name is `roles` then override classic extended multiple select #} {% if field_name == 'roles' %} <div class="form-group"> {# Render form field name #} <label for="roles_field" class="col-sm-3 control-label" > {{ field_name|capitalize }} </label> <div id="roles_field" class="sonata-ba-field col-sm-9 sonata-ba-field-standard-natural" > {# Render each groups role #} {% for label,choices in form.roles.vars.choices %} {# Render group role name #} <label> <b>{{ label }}</b> </label> {# Render each roles of a group #} {% for key,choice in choices %} {{ form_widget(form.roles.children[key]) }} {{ form_label(form.roles.children[key]) }} {% endfor %} {% endfor %} </div> </div> {% else %} {{ form_row(form[field_name])}} {% endif %}
然后角色表单将显示如下