0

Sonata admin $formMapper choice: How to make all or few of the choices are selected / checked by default.

/**
 * @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
  ));
}
dan1st
  • 12,568
  • 8
  • 34
  • 67
siva
  • 315
  • 2
  • 11

1 Answers1

0

Finally, I found the answer.

/**
 * @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
  ));
}

Note In 'data' we have to give the index of the selected value. Let us say if i want to only the few values to be selected then we have to pass those values indexes. From the above example if i want 10, 40 as selected the then we have to prepare $selectedDenominations is like array(0, 3).

siva
  • 315
  • 2
  • 11