如何在 choiceType sonata admin bundle 上设置选定的选项

How to set selected option on choiceType sonata admin bundle

在 symfony3 奏鸣曲管理包中为选择字段设置默认/选定选项

例如:

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
  ]);
}

如何将_permanent_设置为选中值?

这个post对我没有帮助 setting default value in symfony2 sonata admin bundle

你可以试试这样:

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
  ]);
}