Symfony2 Multiple Forms: Different From Embedded Forms

One thing to have clear in mind is that embedded forms is different from having multiple forms in one page.
With embedded forms single or collection the problem that is solved is always submission of a single form composed of different entities.
With multiple forms the problem that is solved is submission of multiple forms persisting different sets of entities and properties depending to which form is submitted.

So here is the format that does the trick for multiple forms. I makes use of the createNamedBuilder method which gets the type forms for an entity of class A. It also receives as parameter the name of the form we would like to label it. So we define two forms in the code below, each including two different properties of different entity, although they could be the same. Then we make sure we receive the POSTed information writing a conditional from where we can handle each form separate and persist each corresponding entity discriminating according to which form was submitted. Lastly we see that we pass to the twig engine two variables for two separate forms.

public function multiformAction()
{
    $form1 = $this->get('form.factory')->createNamedBuilder($formTypeA, 'form1name')
        ->add('foo', 'text')
        ->getForm();
 
    $form2 = $this->get('form.factory')->createNamedBuilder($formTypeB, 'form2name')
        ->add('bar', 'text')
        ->getForm();
 
    if('POST' === $request->getMethod()) {
 
        if ($request->request->has('form1name') {
            // handle the first form
        }
 
        if ($request->request->has('form2name') {
            // handle the second form
        }
    }
 
    return array(
        'form1' => $form1->createView(),
        'form2' => $form2->createView()
    );  
}

Notice also that the $request variable is the sf2 request object. This object has a property called request too. The type of this property is ParameterBag which in turn has a method called has which checks for the given key within the object.

Within the twig templates obviously we are going to call separately the code for each form like:

{{ form_row(form1.foo) }}
...
{{ form_row(form2.bar) }}

That is it. Thanks for donating!

8 thoughts on “Symfony2 Multiple Forms: Different From Embedded Forms

  1. BE AWARE: the order of the arguments has changed in symfony 2.1

    public FormBuilderInterface createNamedBuilder(string $name, string|FormTypeInterface $type = ‘form’, mixed $data = null, array $options = array(), FormBuilderInterface $parent = null)

  2. Pingback: Symfony – create two forms in one action?CopyQuery CopyQuery | Question & Answer Tool for your Technical Queries,CopyQuery, ejjuit, query, copyquery, copyquery.com, android doubt, ios question, sql query, sqlite query, nodejsquery, dns query, up

  3. if ($request->request->has(‘form1name’)) {
    // handle the first form
    }

    in the if sentence, you forgot to close the last bracket.

  4. Watch out for different in branches . I found that without any reason some strange changes come in newer version of SYMFONY. example branch 2.6

    public FormBuilderInterface createNamedBuilder(string|int $name, string|FormTypeInterface $type = ‘form’, mixed $data = null, array $options = array())

    but in branch 2.0

    public FormBuilder createNamedBuilder(string|FormTypeInterface $type, string $name, mixed $data = null, array $options = array())
    What cause my headache.

Leave a Reply

Your email address will not be published. Required fields are marked *