Symfony2 - Multiple forms in one action

11,298

Solution 1

If your forms are related and need to be processed and validated at once, consider using embedded forms. Otherwise, use a separate action for each form.

If you need to provide a select field to choose a user from existing ones, consider using an entity type field.

Solution 2

I know that has been a long time since the answer, but maybe if someone is looking for it this could be helpfull.

I've got two forms: user_form and company_form, and the submit has take place in the same function of the controller. I can know wich form has been submit with the follow code:

    if ($request->getMethod() == 'POST') {

       $data = $request->request->all();

        if (isset($data['user_form'])) //This if means that the user_form has been submit.
        { 

The company_form will pass through the else.

Share:
11,298
Naelyth
Author by

Naelyth

Updated on June 04, 2022

Comments

  • Naelyth
    Naelyth almost 2 years

    I implemented a page to create an instance of an entity and a user related to this. My problem is to bind the request after the submit.

    Now i have this :

    $formA = $this->createForm(new \MyApp\ABundle\Form\AddObjectForm());
    $formB = $this->createForm(new \MyApp\UserBundle\Form\AddUserForm());
    
    if ($request->getMethod() == 'POST')
    {
        $formA->bindRequest($request);
        $formB->bindRequest($request);
    
        if ($formA->isValid() && $formB->isValid())
        {
        }
        // ...
    }
    

    With formA and formB extends AbstractType. But, naturally, $formA->isValid() returns false. How can I do to "cut" the request for example ?