简体   繁体   English

在树枝Symfony中选择HTML标签

[英]Select option html tag in twig Symfony

For my project I used Symfony framework. 对于我的项目,我使用了Symfony框架。 I need to use the select option to generate a list for my form. 我需要使用select选项为我的表单生成一个列表。

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: 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. 问题是当我在表单上选择一个选项时,该字段未加载到数据库中。

I think the problems begin in the controller. 我认为问题始于控制器。 If the controller is called via a get and not a post, then $inforequest is empty. 如果通过get而不是post调用控制器,则$ inforequest为空。 Where does the data come from if the user is not posting a form? 如果用户未发布表单,数据从何而来?

Then, as far as I'm aware, the Request object should always be injected as the first variable in the function call. 然后,据我所知,应始终将Request对象作为函数调用中的第一个变量注入。

If those are sorted out, then you should be able to set the default value in twig. 如果这些都被整理了,那么您应该能够在树枝中设置默认值。 Something like this: 像这样:

    <select name="nature" id="nature">
        <option value ="Other" {% if inforequest.nature == 'Other' %} selected="selected" {% endif %}>Other</option>
        <option value ="Adn" {% if inforequest.nature == 'Adn' %} selected="selected" {% endif %}>ADN</option>  
    </select>

Symfony can do this all automatically: Try changing the option for 'nature' in your formType to: Symfony可以自动完成所有操作:尝试将formType中的'nature'选项更改为:

     ->add('nature', 'choice', array('label' => '(*) Nature sample', 'choices' => array ('Other' => 'Other', 'Adn' => 'ADN')))

And in your twig, replace the select with: 然后在您的树枝中,将select替换为:

{{form_errors(form.nature)}}
{{form_widget(form.nature)}}

Finally: The full reference is here: http://symfony.com/doc/current/book/forms.html 最后:完整的参考资料在这里: http : //symfony.com/doc/current/book/forms.html

Problem resolved! 问题解决了! I didn't put the correct name on my select option id 我没有在选择选项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). 精度低:我的bs_bundle_sample是我的formType的名称(在我的getName函数上找到名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM