8

I have a template with multiple carts. There can be a variable amount of carts, there's no fixed limit.

In each cart I want to have a form where the user can select a country. If he submits the form the shipping costs should be established.

Now I'm doing the following to achieve it in twig:

{% for cart in carts %}
    {# Some template stuff #}
    {{ form_start(form) }}
        <div class="form-input">
        <label for="country" class="middle-color">Country <span class="active-color">*</span></label>
        {{ form_widget(form.country) }}
    {{ form_end(form) }}
{% endfor %}

This is my form builder:

$form = $this->createFormBuilder()
    ->add('country', 'choice', array('choice_list' => $choiceList, 'label' => 'country',
        'attr' => array('class' => "custom-selectbox dark-color light-gradient")))
    ->getForm();

Now the problem is that this logic works fine for the first cart, but there's no form displayed for further carts. How can I deal with this?

3 Answers 3

17

I came across this and another question about the similar issue. You can find my first answer for a solution here.

To wrap it up, I did not call the createView() function on the form in the controller, as usually done when passing the form to the view, but in the twig view itself.

E.g. in your controller action you do return the form object itself:

return $this->render('AppBundle:Cart:list.html.twig', ['formObject' => $form];

and in your view you would set the form in each loop:

{% for cart in carts %}
    {# Some template stuff #}
    {% set form = formObject.createView %}
    {{ form_start(form) }}
        <div class="form-input">
        <label for="country" class="middle-color">Country <span class="active-color">*</span></label>
        {{ form_widget(form.country) }}
    {{ form_end(form) }}
{% endfor %}
Sign up to request clarification or add additional context in comments.

2 Comments

{% set form = formObject.createView %} throws an error Array to string conversion. Do you have a solution for Symfony 5.* ?
For symfony5+ what I done is giving the form builder and to do "builder.form.createView" in the twig
0

For Symfony 5 and 6 you can do

$formBuilder = $this->createFormBuilder()->add('...');

return $this->render('list.html.twig', ['formBuilder' => $formBuilder];

and in your twig

{% for cart in carts %}
    {# Some template stuff #}
    {% set form = formBuilder.form.createView %}
    {{ form_start(form) }}
        <div class="form-input">
        <label for="country" class="middle-color">Country <span class="active-color">*</span></label>
        {{ form_widget(form.country) }}
    {{ form_end(form) }}
{% endfor %}

Comments

-1

You should use collection form type. Here is a guide to start with How to Embed a Collection of Forms

P.S. Notice that after rendering a form widget Form component marks it as rendered and does not render once more.

1 Comment

I don't see how this will help me. I create a choice_list depend on external data. I don't want to display the choice_list multiple times but the whole form multiple times

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.