有没有办法将未选择的数据直接发送到控制器?

我有一个带有ChoiceType字段的表单,我需要对选定的数据做一些事情,而对未选定的数据做一些事情。


我想我需要使用选定的数据来检索未选定的数据,但是有没有办法直接将它们发送到控制器?也许有,empty_data但我不确定如何使用它。


为了澄清我是否在 vue 中有它:


<select name="carlist" form="carform" multiple>

  <option value="volvo" selected>Volvo</option>

  <option value="saab" selected>Saab</option>

  <option value="opel">Opel</option>

  <option value="audi">Audi</option>

</select>

如何使用 检索“audi”和“opel”值$form->getData()?(当然如果可能的话)


MM们
浏览 126回答 2
2回答

芜湖不芜

如果字段是表单对象的一部分($form在本例中),要检索字段的数据,您carlist可以使用:$carlist&nbsp;=&nbsp;$form->get('carlist');或者,要从整个表单中获取数据,您可以使用:$data = $form->getData().如果您的字段不是表单对象的一部分,您可以使用:$carlist&nbsp;=&nbsp;$request->request->get('carlist');这样做是从Request 对象中获取数据。

手掌心

你有一个表格,像这样:<select name="carlist" form="carform" multiple>&nbsp; <option value="volvo" selected>Volvo</option>&nbsp; <option value="saab" selected>Saab</option>&nbsp; <option value="opel">Opel</option>&nbsp; <option value="audi">Audi</option></select>你的控制器:&nbsp; &nbsp; $form = $this->createForm(YourForm::class, //...);&nbsp; &nbsp; $choices = [&nbsp; &nbsp; &nbsp; &nbsp; 'Volvo' => 'volvo',&nbsp; &nbsp; &nbsp; &nbsp; 'Saab'&nbsp; => 'saab',&nbsp; &nbsp; &nbsp; &nbsp; 'Opel'&nbsp; => 'opel',&nbsp; &nbsp; &nbsp; &nbsp; 'Audi'&nbsp; => 'audi',&nbsp; &nbsp; ];&nbsp; &nbsp; $form->add('carlist', ChoiceType::class, [&nbsp; &nbsp; &nbsp; &nbsp; 'multiple' => true,&nbsp; &nbsp; &nbsp; &nbsp; 'choices' => $choices&nbsp; &nbsp; ]);&nbsp; &nbsp; $form->handleRequest($request);&nbsp; &nbsp; if ($form->isSubmitted() && $form->isValid()) {&nbsp; &nbsp; &nbsp; &nbsp; $not_used_cars = [];&nbsp; &nbsp; &nbsp; &nbsp; $car_list = $form->get('carlist')->getData();&nbsp; &nbsp; &nbsp; &nbsp; foreach ($choices as $car) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( false === array_search( $car, $car_list ) )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $not_used_cars[] = $car;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* dump($not_used_cars);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* //Output:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; 0 => 'opel'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; 1 => 'audi'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP