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 ios可以实现ymodem-1k协议 1024字节传输吗?
  • ¥300 寻抓云闪付tn组成网页付款链接
  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)