symfony how get selected data from select

2019-07-26 11:41发布

Entity Type is defined as follow in my form:

  ->add('esame_' . $i, EntityType::class, array(
      'label'        => false,
      'mapped'       => false,
      'class'        => 'AppBundle:Nome_esame',
      'required'     => true,
      'multiple'     => true,
      'choice_label' => 'nome',
//    'disabled'     => 'disabled',
      'attr'         => array(
          'placeholder' => 'Esami',
          'class' => 'max_width esame_row select_esame',
//        'class'=>'col-md-12 col-md-offset-0  col-xs-9 col-xs-offset-3 ',
      )
  ))

In my form submit, I want to take all the selected value and create a new entity from every one. I tried this:

  foreach($form->get('esame_0')->getData() as $value){
          $field= new Field();
          $field->setvalue($value);  // ->$value i want is the val of selected option
         }

but $form->get('esame_0')->getData() does not return the selected data..

How can I do it?

1条回答
迷人小祖宗
2楼-- · 2019-07-26 12:08

The value that comes with EntityType is an Entity object. Try this way.

$entityObject = $form->get('esame_0')->getData()
$data = $entityObject->getId() or $entityObject->(Entity getter function)
查看更多
登录 后发表回答