Symfony2 : Two forms in a same page

前端 未结 7 780
感情败类
感情败类 2020-12-05 23:50

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

7条回答
  •  抹茶落季
    2020-12-06 00:28

    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.

提交回复
热议问题