我使用easyadmin,我希望"role“字段显示字段类型"radio”中的角色选择,但是发生了这种类型的错误(数组到字符串的转换)(见下图):
Notice: Array to string conversion
下面是我的配置:
easy_admin:
entities:
User:
class: AppBundle\Entity\User
form:
fields:
- { property: 'username' }
- { property: 'email' }
- { property: 'enabled' }
- property: 'plainPassword'
type: 'repeated'
type_options:
type: Symfony\Component\Form\Extension\Core\Type\PasswordType
required: false
first_options: { label: '%label.password%' }
second_options: { label: '%label.password_confirmation%' }
invalid_message: fos_user.password.mismatch
- property: 'roles'
type: 'choice'
type_options:
mapped: true
expanded: true
multiple: false
choices: { 'Conseiller': 'ROLE_USER', 'Administrateur': 'ROLE_ADMIN' }有人会提供一个解决方案给我,这样我就可以用easyadmin显示单选按钮?
提前感谢
发布于 2018-08-25 01:56:18
这里有一个Symfony 3.4中的解决方案(使用Yes/No下拉菜单),可能会有所帮助:
在config.yml中
imports:
...
- { resource: easyAdmin.yml }在easyadmin.yml中
fields:
...
- property: 'hasRoleAdmin'
label: 'Is admin?'
type: choice
type_options:
choices:
'No': 'No'
'Yes': 'Yes' 在用户实体中:
public function hasRoleAdmin()
{
return ($this->hasRole('ROLE_ADMIN')) ? 'Yes' : 'No';
}
public function setHasRoleAdmin($isAdmin)
{
if ('Yes' === $isAdmin && 'No' === $this->hasRole('ROLE_ADMIN')) {
$this->addRole('ROLE_ADMIN');
}
if ('No' === $isAdmin && 'Yes' == $this->hasRole('ROLE_ADMIN')) {
$this->removeRole('ROLE_ADMIN');
}
$this->isAdmin = $isAdmin;
}发布于 2019-05-14 19:03:35
@johan-rm你所做的几乎是正确的。
事实上,您不能对角色使用单选按钮,因为角色(参见s)是多项选择域。您需要使用复选框(或多个选择)。
在您的代码中,唯一错误的部分是:multiple: false。如果您试图将一个数组映射到一个选择字段,那么您就是在尝试将一个数组映射到一个字符串,因此出现了错误。只需将multiple: false更改为multiple: true。
这就是结果:
easy_admin:
entities:
User:
class: AppBundle\Entity\User
form:
fields:
- { property: 'username' }
- { property: 'email' }
- { property: 'enabled' }
- property: 'plainPassword'
type: 'repeated'
type_options:
type: Symfony\Component\Form\Extension\Core\Type\PasswordType
required: false
first_options: { label: '%label.password%' }
second_options: { label: '%label.password_confirmation%' }
invalid_message: fos_user.password.mismatch
- property: 'roles'
type: 'choice'
type_options:
mapped: true
expanded: true
multiple: true
choices: { 'Conseiller': 'ROLE_USER', 'Administrateur': 'ROLE_ADMIN' }https://stackoverflow.com/questions/52006043
复制相似问题