1

Is there a solution to handle multiple forms on one Page in Symfony 3?

Its basically working but submitting one form end in null values for the other form since all the forms seem to have the same name (form).

I have multiple handleRequests on the page, each for one form

$form->handleRequest($request);

...

$secondform->handleRequest($request);

I think I would need a possibility to check which form was submitted but

$request->request->get($form->getName())

gives the same name for both forms so this doesn’t work.

Is there a solution to handle multiple forms or maybe to change the name of each form?

3
  • Possible duplicate: stackoverflow.com/questions/23994563/… If the given solution isn't working for yo, please share some of your code. Commented Apr 29, 2016 at 18:42
  • You can also give your submit buttons different names and key off of that: symfony.com/doc/current/book/… Commented Apr 29, 2016 at 21:00
  • The solution isn't working as it is for Symfony2 and I'm still searching for the possibility to give a name to a form in Symfony3. I tried to use $request->request->get($form->getName()) to find out if one form is submitted in the request but to do that each form needs to have a unique name and all forms are of the name of "form". Commented Apr 30, 2016 at 11:34

1 Answer 1

2

I ran into this problem when dealing with multiple dynamically generated forms on one page. isSubmitted() was not behaving sanely, as I believe it checks based on form names, and all the forms had the same name. As such, submitting one form updated all the forms with that form's data.

I got around this using the Form Factory's createNamed method.

This was tested on Symfony v3.2.1

foreach ($aObjects as $oObject) 
{
    $sUniqueFormName = 'Form' . $oObject->getId(); //Use some unique data to generate the form name.
    $oForm = $this->get('form.factory')->createNamed($sUniqueFormName, CustomFormType::class, $oObject);
    $aForms[$sUniqueFormName] = $oForm;
}

foreach ($aForms as $sFormName => $oForm)
{
    $oForm->handleRequest($oRequest);
    if ($oForm->isSubmitted() && $oForm->isValid())
    {
        //Do stuff
    }
    //Create the view *after* calling handleRequest, so data is updated to the form, even if, say, there are validation errors.
    $aFormViews[$sFormName] = $oForm->createView(); 
}

The template is pretty self-explanatory, just iterate over the form views and render them normally. Make sure you explicitly output a form_end() for each form, though, I experienced weirdness when just using form_rest().

Sign up to request clarification or add additional context in comments.

1 Comment

This is a neat solution, which I used successfully. Remember: once you've assigned a name to a form, you can get it back with $form->getName()--so there's no need to make a note of the name in a variable, if you so wish. Also, you may like to index each form-view by the ID of the entity to which it relates: this makes it easier to access form-views in the template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.