3

I'm stuck with the following problem. I have 3 forms on one page (might be more in the future). when I submit them.. nothing happens (inserting data in the DB) and the other 2 forms get their fields filled in. Might be because they all have a 'name' field? How can I solve this.. so that each forms has is own 'function' and it doesn't interfere with the other forms.

My twig:

<div class="box">
            <h2>Form1</h2>
            {{ form_start(form1) }}
            {{ form_widget(form1) }}
            {{ form_end(form1) }}
        </div>
        <div class="box">
            <h2>Form2</h2>
            {{ form_start(form2) }}
            {{ form_widget(form2) }}
            {{ form_end(form2) }}
        </div>
        <div class="box">
            <h2>Form3</h2>
            {{ form_start(form3) }}
            {{ form_widget(form3) }}
            {{ form_end(form3) }}
        </div>

My controller:

if ($request->isMethod('POST')) {

    $form1->handleRequest($request);
    $form2->handleRequest($request);
    $form3->handleRequest($request);

    if ($form1->isSubmitted() && $form1->isValid() && $request->request->has('form1')) {
        // Do data insert

        //Return to page
    } else if ($form2->isSubmitted() && $form2->isValid() && $request->request->has('form2')) {
        // Do data insert

        //Return to page
    } else if ($form3->isSubmitted() && $form3->isValid() && $request->request->has('form2')) {
        // Do data insert

        //Return to page
    }
}
6
  • 2
    You dont need this $request->request->has('form1') since you already have $form1->isSubmitted(). Also check that the submitted forms are valid, they might just not be validating. Commented May 26, 2017 at 14:46
  • 1
    Is there a reason to have all three forms processed by the same method? I did a similar thing by having 3 forms on a single page, but each form had its own controller method. Commented May 26, 2017 at 14:49
  • This might be helpful. Commented May 26, 2017 at 15:10
  • If you have more than one form pointing to the same controller/method this would mean that you only need one form with different submit buttons and checks then which button is clicked. Commented May 26, 2017 at 15:12
  • Removing $request->request->has('form1' did indeed help with values gettin in the other forms. form1 gets submitted perfectly, but when I try to submit the other 2.. they seems to point to the first form. They try to insert their data in that query - which is ofcourse not ok. Commented May 26, 2017 at 17:08

1 Answer 1

7

I think you don't need to put them all in the same if. What you can do is to separate them like this:

public function whateverAction(Request $request) {
    $form1 = $this->createForm(...);
    $form2 = $this->createForm(...);
    $form3 = $this->createForm(...);

    $form1->handleRequest($request);
    if ($form1->isSubmitted() && $form1->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($obj1); //of whatever the entity object you're using to create the form1 form
        $em->flush();
    }

    $form2->handleRequest($request);
    if ($form2->isSubmitted() && $form2->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($obj2); //of whatever the entity object you're using to create the form2 form
        $em->flush();
    }

    $form3->handleRequest($request);
    if ($form3->isSubmitted() && $form3->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($obj3); //of whatever the entity object you're using to create the form3 form
        $em->flush();
    }

    return $this->render('...', [
        'form1'=>$form1->createView(),
        'form2'=>$form2->createView(),
        'form3'=>$form3->createView(),
    ]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

That's what I did initially, but for some reason when I fill in form 3 and press submit it uses the logic (query) that is behind form 1. Might that because all 3 forms use 'name' in their form?
@Rafie You have 3 different Entities, right? And for each entity, you have 3 different Forms, right? If yes, then in each of the Forms, the method getBlockPrefix() needs to return something like appbundle_entityname. And then, each html form, will have a different name, so in this way there is no confusion between the forms. Assuming you are using symfony >= v2.8 (don't remember exactly though from which version on this is valid).
I don't understand it completely.. I'm totally new to the symfony framework (specially working with forms). My version is 3.2.6
@Rafie Then you should first get used a bit more with Symfony. Anyways, entities are table representations in the code. So inside the controller, you have to have something like: $person = new Person(); where Person is the name of the entity. Then, to create the $form1 form, you'd do $form1 = $this->createForm(PersonType::class, $person);. PersonType is the form, and the Person is the entity which will house the data sent when you press the submit button. In the AppBundle/Form/PersonType you need to have the getBlockPrefix method.
Just tried with 2 forms on the same entity, works like a charm.

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.