Symfony2 - Multiple Forms in One Action

I implemented a page to instantiate an object and the user associated with this. My problem is to bind the request after sending.

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()) { } // ... } 

In formA and formB extends AbstractType. But naturally, $formA->isValid() returns false. How can I do to “cut” a request, for example?

+4
source share
2 answers

If your forms are linked and need to be processed and verified immediately, consider using inline forms . Otherwise, use a separate action for each form.

If you need to specify a selection field to select a user from existing ones, consider using a type field.

+7
source

I know that a lot of time has passed since the answer, but perhaps if someone is looking for it, it can be useful.

I have two forms: user_form and company_form, and the view is executed in the same controller function. I can know which form was submitted using the following 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 profile will go through else.

+5
source

Source: https://habr.com/ru/post/1379517/


All Articles