带有CollectionType/ChoiceType的Double ListBox用于用户角色

发布于 2025-01-25 11:55:45 字数 2420 浏览 1 评论 0原文

使用Symfony 4,我没有这个问题:使用ListBox选择或编辑用户角色的简单表格。

由于我在编辑用户配置文件时使用Symfony 5(具有相同代码)启动一个新项目,因此我有一个类似的列表框:

表单示例

用户formType的一部分:

$roles = [User::USER_DEFAULT => 'Simple User',
            User::USER_ADMIN_APP => 'App. Admin',
            User::USER_ADMIN_PROJECT => 'Project Admin',
            User::USER_ADMIN => 'Administrator'];

$builder->add('username')
            ->add('roles', CollectionType::class, [
                'label' => 'Roles', 'required' => true, 'entry_type' => ChoiceType::class,
                'entry_options' => ['label' => false, 'choices' => array_flip($roles)],])
            ->add('password')
            ->add('email')
            ->add('save', SubmitType::class);

在数据库中,我有一个经典的JSON格式,用于用户角色

#[ORM\Column(type: 'json')]
private $roles = [];

,我创建了一些数据,我创建了:

数据库

我在此页面中创建了一个github示例: form-tests-with-collectionType

我知道,我想念什么,但是什么?我应该使用数据变压器或映射器只有一个listbox选择

感谢您的灯光。 ;)

更新从2022-05-04:

从我的旧SF4项目到一个新的SF5项目,角色字段的唯一更改是Doctrine中的“数组”到“ JSON”:

//Migration file with "Json"
roles CLOB NOT NULL --(DC2Type:json)

//Migration file with "Array"
roles CLOB NOT NULL --(DC2Type:array)

现在,我,我在数据库中使用“ array”:

a:1:{i:0;s:14:"ROLE_ADMIN_APP";}

“ json”之前的角色字段中有一个新格式,而我在固定装置中的错误“ cole_user”:

["ROLE_USER","ROLE_ADMIN_APP"]

随着此更改,我有一个单个列表框,只有一个选择用户:最后:

<select id="user_roles_0" name="user[roles][0]">
   <option value="ROLE_USER">Simple User</option>
   <option value="ROLE_ADMIN_APP">App. Admin</option>
   <option value="ROLE_ADMIN_PROJECT" selected="selected">Project Admin</option>
   <option value="ROLE_ADMIN">Administrator</option>
</select>

最后,我可以使用CollectionType和ChoiceType创建或编辑具有多个角色的ChoiceCetype ...

希望它对像我这样的未来Devs/Noob有帮助;)

Cheers!

With Symfony 4, I don't have this problem : a simple form to choose or edit the role of an user with a listbox.

Since I start a new project with Symfony 5 (with same code) when I edit a user profile, I have a double listbox like this :

Form example

One part of the User FormType :

$roles = [User::USER_DEFAULT => 'Simple User',
            User::USER_ADMIN_APP => 'App. Admin',
            User::USER_ADMIN_PROJECT => 'Project Admin',
            User::USER_ADMIN => 'Administrator'];

$builder->add('username')
            ->add('roles', CollectionType::class, [
                'label' => 'Roles', 'required' => true, 'entry_type' => ChoiceType::class,
                'entry_options' => ['label' => false, 'choices' => array_flip($roles)],])
            ->add('password')
            ->add('email')
            ->add('save', SubmitType::class);

In the database, i have a classical json format for user roles

#[ORM\Column(type: 'json')]
private $roles = [];

Some data from the fixture, I've created :

Database

I have created a GitHub fresh example in this page : form-tests-with-CollectionType

I know, I miss something but what ?? Should I use Data Transformers or Mappers to have only one listbox choice ?

Thanks for your lights. ;)

Update from 2022-05-04 :

From my old SF4 project to a new SF5 project, the only one change for the Roles field is "Array" to "Json" in Doctrine :

//Migration file with "Json"
roles CLOB NOT NULL --(DC2Type:json)

//Migration file with "Array"
roles CLOB NOT NULL --(DC2Type:array)

Now, I have a new format in the database for Roles field with "Array" :

a:1:{i:0;s:14:"ROLE_ADMIN_APP";}

Before with "Json", with my mistake in the fixture "ROLE_USER" :

["ROLE_USER","ROLE_ADMIN_APP"]

And with this change, I have a single listbox with ONLY ONE CHOICE for a user :

<select id="user_roles_0" name="user[roles][0]">
   <option value="ROLE_USER">Simple User</option>
   <option value="ROLE_ADMIN_APP">App. Admin</option>
   <option value="ROLE_ADMIN_PROJECT" selected="selected">Project Admin</option>
   <option value="ROLE_ADMIN">Administrator</option>
</select>

Finally, I can create or edit a user with a CollectionType and ChoiceType with multiple roles...

Hope it help for future devs/noob like me ;)

Cheers !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落在眉间の轻吻 2025-02-01 11:55:45

您可以尝试一下:

$roles = [
    'Super Admin' => 'ROLE_SUPER_ADMIN',
    'Admin' => 'ROLE_ADMIN',
    ...
];

$builder->add('roles', ChoiceType::class, [
    'label' => 'form.label.role',
    'choices' => $roles,
    'required' => true,
    'multiple' => true,
    'expanded' => false
]);

我的key =&gt; Value是相反的,我正在直接使用ChoceType。

You can try this :

$roles = [
    'Super Admin' => 'ROLE_SUPER_ADMIN',
    'Admin' => 'ROLE_ADMIN',
    ...
];

$builder->add('roles', ChoiceType::class, [
    'label' => 'form.label.role',
    'choices' => $roles,
    'required' => true,
    'multiple' => true,
    'expanded' => false
]);

My key => value is reversed and I'm using the ChoiceType directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文