我正在使用Symfony 3.4和FosUserBundle。为了包含角色
,我自定义了注册表单:
<?php
$form->add('roles', ChoiceType::class, array(
'choices' => array(
'user' => 'ROLE_USER',
'admin' => 'ROLE_ADMIN'
),
'label' => 'Role :',
'expanded' => true,
'multiple' => true
));
现在在注册表单中通过2个新的复选框选择角色...但我想使用<select>
标签来显示它。当我将expanded
选项设置为false
时,我会得到一个下拉选择框,但如果我将multiple
选项设置为FALSE,我会收到一个错误:
Array to string conversion
有什么解决方法吗?
编辑:下面是堆栈跟踪:
Symfony\Component\Debug\Exception\ContextErrorException:
Notice: Array to string conversion
at vendor\symfony\symfony\src\Symfony\Component\Form\ChoiceList\ArrayChoiceList.php:73
at Symfony\Component\Form\ChoiceList\ArrayChoiceList->Symfony\Component\Form\ChoiceList\{closure}(array('ROLE_USER'))
at call_user_func(object(Closure), array('ROLE_USER'))
(vendor\symfony\symfony\src\Symfony\Component\Form\ChoiceList\ArrayChoiceList.php:158)
at Symfony\Component\Form\ChoiceList\ArrayChoiceList->getValuesForChoices(array(array('ROLE_USER')))
(vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer.php:32)
at Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer->transform(array('ROLE_USER'))
(vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:1104)
at Symfony\Component\Form\Form->normToView(array('ROLE_USER'))
(vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:350)
at Symfony\Component\Form\Form->setData(array('ROLE_USER'))
(vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper.php:49)
at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(User), object(RecursiveIteratorIterator))
(vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:383)
at Symfony\Component\Form\Form->setData(object(User))
(vendor\friendsofsymfony\user-bundle\Controller\RegistrationController.php:70)
at FOS\UserBundle\Controller\RegistrationController->registerAction(object(Request))
at call_user_func_array(array(object(RegistrationController), 'registerAction'), array(object(Request)))
(var\cache\dev\classes.php:4659)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(var\cache\dev\classes.php:4614)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:200)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(web\app_dev.php:29)