Symfony3 Render several times in the same form

I would like to display the same form several times in order to handle the same action for two different tabs. The problem is that when I try, only the form of the first tab is displayed, an event if I change the identifier and name of the form. I discovered this expected symfony behavior, but I still need it to work.

I found that it can work with the collection, but I don’t understand how it will work.

twig:

{{ form(contactForm, {'attr': {'id': 'contactFormId' ~ Client.Id}, 'name': "contactFormName" ~ Client.Id})}} 

the form:

 $this->contactForm = $this->createFormBuilder($contact, array('allow_extra_fields' =>true)) ->add('Nom', TextType::class, array('mapped'=>false)) ->add('Prenom', TextType::class, array('mapped'=>false)) ->add('Telephone', TextType::class, array( 'label' => 'TΓ©lΓ©phone')) ->add('Email', TextType::class) ->add('Ajouter', SubmitType::class) ->getForm(); 
+4
source share
2 answers

This is an older question, but I just ran into it, faced with a similar situation. I wanted to have multiple versions of the same form object as a list. For me, the solution was to move the createView() call on the form object to the view instead of calling it in the controller. This is kind of a dirty decision regarding separation of problems, but I decided to publish it so that it can help others anyway.

My controller action looks like this:

 /** * @Route("", name="cart_show") * @Method("GET") */ public function showAction(Request $request) { /** @var CartInterface $cart */ $cart = $this->get('rodacker.cart'); $deleteForm = $this->createDeleteForm(); return $this->render( 'AppBundle:Cart:show.html.twig', ['cart' => $cart, 'deleteForm' => $deleteForm] ); // ... private function createDeleteForm() { return $this->createForm( OrderItemDeleteType::class, null, [ 'action' => $this->generateUrl('cart_remove_item'), 'method' => 'DELETE', ] ); } } 

and in the view, I set the form variable by calling the createView function on the form variable ( deleteForm ) passed from the controller:

 {% for item in items %} {% set form = deleteForm.createView %} {{ form_start(form) }} {{ form_widget(form.item, {'value': item.image.filename}) }} <button type="submit" class="btn btn-xs btn-danger" title="Artikel entfernen"> <i class="fa fa-trash-o"></i> entfernen </button> {{ form_end(form) }} {% endfor %} 
+2
source

Once you create a Symfony form, the same form will not be displayed again.

I would suggest creating a form class and calling Controller :: createForm () several times to create the desired number of instances of Form; you can call isSubmitted, etc. on all forms independently. http://symfony.com/doc/current/book/forms.html#creating-form-classes

0
source

All Articles