Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Multiple forms in one action

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 ?

like image 510
Naelyth Avatar asked Dec 16 '22 07:12

Naelyth


1 Answers

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.

like image 140
Elnur Abdurrakhimov Avatar answered Jan 09 '23 15:01

Elnur Abdurrakhimov