【问题标题】:Symfony select by defaut various optionsSymfony 默认选择各种选项
【发布时间】:2016-11-06 07:04:34
【问题描述】:

我正在使用 symfony 处理一个表单,并且在一个选择中,我希望默认选择一些选项。

我试过了:

->add('idgroupe', 
      ChoiceType::class, 
      array('label' => 'Groupes', 
            'attr' => array('expanded'=> false, 
                            'data'=>$g, 
                            'multiple'=>'true', 
                            'class' => 'form-control'
                            ), 
            'choices'  => $groupes
            )
      )

其中 $g 是一个数组,如:

array(1) { [0]=> string(18) "Vue1",[1]=> string(18) "Vue2" }

但是我遇到了这个错误:

在渲染模板期间引发了异常 ("注意:数组到字符串的转换")

【问题讨论】:

  • expandedmultipledata 不应位于选项数组的 attr 部分,而应与 attr 本身处于同一级别。还有"true" !== true(去掉引号)。

标签: php forms symfony options


【解决方案1】:

尝试:

->add('idgroupe', 
      ChoiceType::class, 
      array('label' => 'Groupes', 
            'attr' => array('class' => 'form-control'
                            ), 
            'expanded'=> false, 
            'data'=>$g, 
            'multiple'=>true, 
            'choices'  => $groupes
            )
      );

【讨论】:

  • 如果你使用 Symfony >= 2.7,请设置选项 'choices_as_values' => true。 symfony.com/blog/…
  • 我的表单有误,但您的解决方案有效。谢谢
  • 嗨 @kimpa2007 如果看起来不错,请随时接受我的回答,谢谢
猜你喜欢
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2016-02-20
  • 1970-01-01
相关资源
最近更新 更多