Home > database >  Saving render result to use it again
Saving render result to use it again

Time:10-05

I'm trying to build a form in my controller, my form contain two "select" options which use the same database table field "villes" I want to to use this field twice, one time for city departure and another time for city arrival

      $form = $this->createFormBuilder($vol)
                 ->add('hor_dep')
                 ->add('villes')
                 ->add('villes')
                 ->add('prix')
                 ->add('reduc', CheckboxType::class, array('required' => true, 'label' => 'Réduction'))
                 ->add('nbr_place')
                 ->getForm();

but when I try to render this form.html.twig

       <div class="col-lg-6">
     {{ form_label(form.hor_dep,'Heure de départ') }}
    {{ form_widget(form.hor_dep) }}
     {{ form_label(form.villes,'Ville de départ') }}
    {{ form_row(form.villes) }}
     {{ form_label(form.hor_arrivee,"Heure d'arrivée") }}
    {{ form_widget(form.hor_arrivee) }}
    {{ form_label(form.villes,"Ville d'arrivée") }}
    {{ form_widget(form.villes) }}
    
</div>
<div class="col-lg-6">
     {{ form_label(form.prix,'Prix') }}
    {{ form_widget(form.prix) }}
     {{ form_label(form.reduc,'possède une réduction ?') }}
    {{ form_widget(form.reduc) }}
     {{ form_label(form.nbr_place,'Nombre de place') }}
    {{ form_widget(form.nbr_place) }}
    </div>

it throws this error

An exception has been thrown during the rendering of a template ("Field "villes" has already been rendered, save the result of previous render call to a variable and output that instead.").

CodePudding user response:

In your case, you have to add two fields in your formType: villeDepart & villeArrivee (like what you did with the heure) because you don't want the first form field be overwritten by the second one.

In your mapping, the two fields will point to the same villes table, but each one will have a different value.

  • Related