1

setting default/ selected option for choice field in symfony3 sonata admin bundle?

For example :

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

/**
 * @inheritdoc
 */
public function configureFormFields(FormMapper $formMapper) {
  parent::configureFormFields($formMapper);

  $formMapper->add('type', ChoiceType::class, [
    'label' => 'config.label_type',
    'choices' => [
      'config.label_permanent' => 'permanent',
      'config.label_automatic' => 'automatic',
      'config.label_temporary' => 'temporary' 
    ],
    'required' => false
  ]);
}

How to make the _permanent_ as selected value ?

This post doesn't help me out setting default value in symfony2 sonata admin bundle

BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105
Theva
  • 873
  • 8
  • 15

1 Answers1

0

You can try with something like this:

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

/**
 * @inheritdoc
 */
public function configureFormFields(FormMapper $formMapper) {
  parent::configureFormFields($formMapper);

  $subject = $this->getSubject();
   if (null === $subject->getId()) {
     $subject->setType('permanent');
   }

  $formMapper->add('type', ChoiceType::class, [
    'label' => 'config.label_type',
    'choices' => [
      'config.label_permanent' => 'permanent',
      'config.label_automatic' => 'automatic',
      'config.label_temporary' => 'temporary' 
    ],
    'required' => false
  ]);
}
Theva
  • 873
  • 8
  • 15
BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105
  • Yes, thanks mate :) . just one correction I have made that, _$this->admin->getSubject();_ should be **$this->getSubject();** – Theva Jul 13 '18 at 15:43