symfony how get selected data from select

2019-07-26 11:54发布

问题:

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:

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)