默认情况下,如何选择所有或少数选项

时间:2017-07-21 11:00:32

标签: sonata-admin choice symfony-2.7

Sonata admin $ formMapper choice:默认情况下,如何选择/选中所有或少数几个选项。

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
  $denominationsList = array(10, 20, 30, 40);
  $formMapper->add('denominations', 'choice', array(
      'choices' => array($denominationsList),
      'multiple' => true,
      'expanded' => true
  ));
}

1 个答案:

答案 0 :(得分:0)

最后,我找到了答案。

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
   $denominationsList = array(10, 20, 30, 40);
   $selectedDenominations = array(0, 1, 2, 3);
   $formMapper->add('denominations', 'choice', array(
      'choices' => array($denominationsList),
      'multiple' => true,
      'expanded' => true,
      'data' => $selectedDenominations
  ));
}

注意'data'中,我们必须提供所选值的索引。让我们说如果我只想要选择几个值,那么我们必须传递这些值索引。从上面的示例中,如果我想要选择10,40,那么我们必须准备$selectedDenominations就像array(0, 3)