我已经阅读了其他主题,但它并没有解决我的问题所以:
我有这个
->add('role', 'choice', array(
'label' => 'I am:',
'mapped' => true,
'expanded' => true,
'multiple' => false,
'choices' => array(
'ROLE_NORMAL' => 'Standard',
'ROLE_VIP' => 'VIP',
)
))
Run Code Online (Sandbox Code Playgroud)
无论我做什么,我都会收到这个错误:
Notice: Array to string conversion in C:\xampp\htdocs\xxx\vendor\symfony\symfony \src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php line 458
Run Code Online (Sandbox Code Playgroud)
在我的表单类型中,甚至没有调用setRole方法(当我将它重命名为某些垃圾时,仍然会发生错误).为什么会这样?
//编辑
完整堆栈跟踪:
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php at line 458 -
*/
protected function fixIndex($index)
{
if (is_bool($index) || (string) (int) $index === (string) $index) {
return (int) $index;
}
at ErrorHandler ->handle ('8', 'Array to string conversion', 'C:\xampp\htdocs \xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php', '458', array('index' …Run Code Online (Sandbox Code Playgroud) 我想使用 ChoiceType 构建一个表单,并且选项值/选择基于数据库表(已经有记录)。
当表单显示时,宗教列表将在下拉列表/组合框中可用。
例子 :
$builder->add('name', ChoiceType::class, array(
'choices' => $religions //List of religions
));
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的代码:
class Religion
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=50)
*/
protected $name;
/*** getter/setter ... ***/
}
Run Code Online (Sandbox Code Playgroud)
/表格/宗教类型
class ReligionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', ChoiceType::class, array(
'choices' => ____________
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' …Run Code Online (Sandbox Code Playgroud)