加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 将角色分组到Sonata表单中的一个复选框

发布时间:2020-12-13 22:26:48 所属栏目:PHP教程 来源:网络整理
导读:我在Sonata面板中有一个表单,可以将角色设置为一个组 我希望在复选框中使用相同的角色字段,因此我添加了’expanded’=对我的领域真实 protected function configureFormFields(FormMapper $formMapper){ $formMapper -add('name') -add('roles',SecurityRole
我在Sonata面板中有一个表单,可以将角色设置为一个组

enter image description here

我希望在复选框中使用相同的角色字段,因此我添加了’expanded’=>对我的领域真实

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name')
        ->add('roles',SecurityRolesType::class,[
            'expanded' => true,])
    ;
}

但我明白了

enter image description here

我正在尝试将此角色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 %}

然后角色表单将显示如下

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读