A.Mali
A.Mali

Reputation: 11

Select option html tag in twig Symfony

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.

Upvotes: 1

Views: 10827

Answers (3)

A.Mali
A.Mali

Reputation: 11

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

Upvotes: 0

Ben Hillier
Ben Hillier

Reputation: 2104

I think the problems begin in the controller. If the controller is called via a get and not a post, then $inforequest is empty. 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.

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>

Upvotes: 5

Ben Hillier
Ben Hillier

Reputation: 2104

Symfony can do this all automatically: Try changing the option for 'nature' in your formType to:

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

And in your twig, replace the select with:

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

Finally: The full reference is here: http://symfony.com/doc/current/book/forms.html

Upvotes: 0

Related Questions