duaj39673 2016-06-13 13:52
浏览 78
已采纳

在twig Symfony中选择选项html标签

For my project I used Symfony framework. I need to use the select option to generate a list for my form.

Here is the code:

Form:

<form method="post" {{ form_enctype(form)}} action="{{ path('my_path')}}">
    {{form_errors(form)}}
    <div name="nature">
        {{form_label(form.nature,"(*) Nature sample")}}
        {{form_errors(form.nature)}}
        <select name="nature" id="nature">
            <option value ="Other">Other</option>
            <option value ="Adn">ADN</option>  
        </select>
    </div>
    {{ form_widget(form) }}
    <input type="submit" value="Next" class="btn btn-primary" />
</form>

FormType:

    public function buildForm(FormBuilderInterface $builder, array $options){
    $builder
            ->add('nature')
            ->add('origin');
     }

Controller:

public function madeDemandAction($id, Request $request)
{
     $em = $this-> getDoctrine() -> getManager();
     $sample = new Sample();
     $repository = $this ->getDoctrine()
                 ->getManager()
                 ->getRepository('BsBundle:Demand')
                 ->find($id);

     $demand = $repository;
     $form=$this ->createForm(new SampleType, $sample);

     if($request ->getMethod() == 'POST')
     {
       $form->handleRequest($request);
       if($form->isSubmitted() && $form->isValid())
       {
         dump($request);
         $inforequest=$form->getData();
         dump($inforequest);
         $em = $this->getDoctrine()->getManager();
         $em->persist($inforequest);
         $em->flush();
         return $this->redirect($this->generateUrl('homepage'));
       }
     }
     return $this ->render('bs_connected/human_demand.html.twig'
     , array('form'=>$form ->createView()
          , 'inforequest'=>$inforequest
          ));
   }

The problem is when I select an option on my form, the field is not load on my database.

  • 写回答

3条回答 默认 最新

  • dousi2251 2016-06-16 15:04
    关注

    Problem resolved! I didn't put the correct name on my select option id

    Here the code

        <select name="bs_bundle_sample[nature]" id="bs_bundle_sample_nature">
            <option value ="Other">Other</option>
            <option value ="Adn">ADN</option>  
        </select>
    

    Small precision : my bs_bundle_sample is the name of my formType (name find on my getName function).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵