A.Mali A.Mali - 8 months ago 174
PHP Question

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


public function buildForm(FormBuilderInterface $builder, array $options){


public function madeDemandAction($id, Request $request)
$em = $this-> getDoctrine() -> getManager();
$sample = new Sample();
$repository = $this ->getDoctrine()

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

if($request ->getMethod() == 'POST')
if($form->isSubmitted() && $form->isValid())
$em = $this->getDoctrine()->getManager();
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.


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>  

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