I\'ve got two forms in a same page.
My problem is when I tried to submit a form, it\'s like it tried to submit the second form below in the page as well.
As
Using Named forms is a viable solution for handling multiple forms, but it can get a little messy, particularly if you're generating forms dynamically.
Another method, as of Symfony 2.3, is to check which submit button was clicked.
For example, assuming that each form has a submit button named 'save'
:
if ('POST' == $Request->getMethod())
{
$form1->handleRequest($Request);
$form2->handleRequest($Request);
$form3->handleRequest($Request);
if ($form1->get('save')->isClicked() and $form1->isValid())
{
//Do stuff with form1
}
if ($form2->get('save')->isClicked() and $form2->isValid())
{
//Do stuff with form2
}
if ($form3->get('save')->isClicked() and $form3->isValid())
{
//Do stuff with form3
}
}
I believe this has a small amount of additional overhead as compared to the named builder method (due to multiple handleRequest
calls), but, in certain cases, it results in cleaner code. Always good to have multiple solutions to choose from. Some of the additional overhead could be alleviated via nested if/else statements, if necessary, but, unless we're talking about dozens of forms per page, the additional overhead is negligible in any case.
Here's an alternate implementation using anonymous functions that minimizes code repetition:
$form1Action = function ($form) use (&$aVar) {
//Do stuff with form1
};
$form2Action = function ($form) use (&$anotherVar) {
//Do stuff with form2
};
$form3Action = function ($form) use (&$yetAnotherVar) {
//Do stuff with form3
};
$forms = [$form1 => $form1Action,
$form2 => $form2Action,
$form3 => $form3Action];
if ('POST' == $Request->getMethod())
{
foreach ($forms as $form => $action)
{
$form->handleRequest($Request);
if ($form->get('save')->isClicked() and $form->isValid())
{
$action($form);
}
}
}
This did the trick for me in Symfony 3 (should also work for Symfony 2):
$form1 = $this->createForm(
MyFirstFormType::class
);
$form2 = $this->createForm(
MySecondFormType::class
);
if ($request->isMethod('POST')) {
$form1->handleRequest($request);
$form2->handleRequest($request);
if ($form1->isSubmitted()) {
// Handle $form1
} else if ($form2->isSubmitted()) {
// Handle $form2
}
}
The problem is that you have two nameless forms (input names like inputname
instead of formname[inputname]
, and thus when you bind the request to your form and it gets validated it detects some extra fields (the other form) and so it is invalid.
The short-term solution is to create a named builder via the form factory, so instead of:
$form = $this->createFormBuilder(null)
you should use:
$form = $this->get("form.factory")->createNamedBuilder("my_form_name")
The long term solution would be to create your own form classes, that way you can keep your form code separate from the controller.
You have to treat the forms separately:
if('POST' === $request->getMethod()) {
if ($request->request->has('form1name')) {
// handle the first form
}
if ($request->request->has('form2name')) {
// handle the second form
}
}
This is perfectly explained in Symfony2 Multiple Forms: Different From Embedded Forms (temporarily unavailable - see below)
As the link provided above is temporarily unavailable, you can see an archive of that resource here.
Look at the blockprefix :
public function getBlockPrefix()
{
return 'app_x_form'.$form_id;
}
The two forms will be posted.
Try using:
$this->createNamedBuilder
instead of
$this->createFormBuilder
Then in your controller, locate the form by name:
if ($request->request->has("your form name") {
$form->handleRequest($request);
}