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条)

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上