带有CollectionType/ChoiceType的Double ListBox用于用户角色
使用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 :
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 :
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试一下:
我的
key =&gt; Value
是相反的,我正在直接使用ChoceType。You can try this :
My
key => value
is reversed and I'm using the ChoiceType directly.