Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 : Two forms in a same page

Tags:

forms

php

symfony

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 follow, you can find my 2 forms :

public function createSuiviForm() {

    return $form = $this->createFormBuilder(null)
            ->add('numero', 'text', array('label' => 'N° : ',
                'constraints' => array(
                    new Assert\NotBlank(array('message' => 'XXXX')),
                    new Assert\Length(array('min' => 19, 'max' => 19, 'exactMessage' => 'XXX {{ limit }} XXX')))))
            ->add('xxxx', 'submit')
            ->getForm();
}

public function createModificationForm() {

    return $form = $this->createFormBuilder(null)
            ->add('modification', 'submit', array('label' => 'XXXXXXXXXXXXXXXXXXXX'))
            ->getForm();
}

My second form as only a submit button.

I passed them to my render and display them by using :

<div class="well">
    <form method="post" action='' {{form_enctype(form)}} >
        {{ form_widget(form) }}
        <input type="submit" class="btn btn-primary"/>
    </form>
    <div class='errors'>
        {{ form_errors(form) }}
     </div>
</div>

'form' is the name of my variable to the first form and 'update' for my second form.

When I attempted to submit my second form, I need to click twice and finally I get :

"This form should not contain extra fields."
And all non valid input for the remainding form.

I tried to add validation_group to false but to no avail.

I don't understand why I got this error because my forms are not embedded at all

I hope you will understand...

like image 759
scamp Avatar asked Jun 02 '14 12:06

scamp


People also ask

Can there be multiple forms in a page?

You can handle multiple separate forms on a single page by including a hidden field identifying the submitted form. You can use formsets to create multiple different instances of the same form on a single page.

Can you have multiple forms within the same HTML document?

Of course it's possible.


Video Answer


2 Answers

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)

Update

As the link provided above is temporarily unavailable, you can see an archive of that resource here.

like image 182
Mick Avatar answered Sep 18 '22 12:09

Mick


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
    }

}
like image 31
totas Avatar answered Sep 20 '22 12:09

totas