NanoPish
NanoPish

Reputation: 1501

symfony 4 multiple forms loop twig submit

I need to have one form by div. I managed to do that but when I submit one of them, it submits them all.


My controller sends an array of forms to the twig template.

First I instanciate all my forms:

$forms = [];
foreach ($data as $index => $something)
{
            // create form
            $formData = new FormData($data);   
            $forms[$index] = $this->createForm(MyForm::class, $formData);
}                                                                                                                                                                                                                        

Once done, I iterate over the forms to handle their submission:

foreach ($forms as $form)                                                                    
{
    $form->handleRequest($request);                                                          
    if ($form->isSubmitted())
    {                                                              
        if ($form->isValid())                                                                
        {
                // I do stuff
        }
    }
}

i think that the problem lies in the code above: I execute the //I do stuff code for each form generated instead of just one. Every form gets submitted, not just the clicked one.

Then, I render the forms:

foreach ($forms as &$form)                                                                   
{                                                                                            
    $form = $form->createView();                                                             
}

And send then to the twig template I render from the controller:

// render template                                                                           
return $this->render('prestations.html.twig', array(                                         
    'forms' => $forms,                                                                       
    'error' => $error                                                                        
));

From the twig template, I loop over the forms:

{% for stuff in stuffs %}
<div>
    <p>{{ loop.index0 }}</p>                                                                 
    {{ form_start(forms[loop.index0]) }}                                                     
    {{ form_widget(forms[loop.index0]) }}                                                    
    {{ form_end(forms[loop.index0]) }}
</div>      
{% endfor %}

The form contains one input field and one submit button.

How to make the submit button submit only one form instead of all forms ?

Upvotes: 3

Views: 3142

Answers (1)

goto
goto

Reputation: 8164

When you submit your forms, all forms are modified if they have the same html name attributes.

To differenciate your forms, you have to name differently your forms, in order to have something like

form1_[input]
[...]
form1_[submit]


form2_[input]
[...]
form2_[submit]

You can create a form than you arbitrary name like this

$forms[$index] = $this->container
    ->get('form.factory')
    ->createNamed('form' . $index, MyForm::class, $formData);

It is exactly like the helper $this->createForm(MyForm::class, $formData); except than the first parameter is the custom form name

Upvotes: 6

Related Questions