pele_mele:stack_exchange:stack_overflow:29987139
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédente | |||
pele_mele:stack_exchange:stack_overflow:29987139 [2024/11/23 04:02] – supprimée - modification externe (Date inconnue) 127.0.0.1 | pele_mele:stack_exchange:stack_overflow:29987139 [2024/11/23 04:02] (Version actuelle) – ↷ Nom de la page changé de pele_mele:stack_exchange:stack_overflow:stackoverflow-29987139 à pele_mele:stack_exchange:stack_overflow:29987139 alexis | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== Symfony2 - Collection type is displayed twice in my view ====== | ||
+ | I have a form with a collection and I can see it properly when I use the following syntax: | ||
+ | |||
+ | <code twig> | ||
+ | form_row(form.items) | ||
+ | </ | ||
+ | |||
+ | Then when I try to render it by myself, it is duplicated. | ||
+ | |||
+ | Here is my code in my twig file: | ||
+ | |||
+ | <code twig> | ||
+ | {{ form_errors(form) }} | ||
+ | {{ form_start(form) }} | ||
+ | |||
+ | ... some fields here ... | ||
+ | |||
+ | <div id="{{ form.items.vars.id }}" class=" | ||
+ | < | ||
+ | <a href="#" | ||
+ | {% for item in form.items %} | ||
+ | {{ form_row(item) }} | ||
+ | {% endfor %} | ||
+ | </ | ||
+ | |||
+ | ... some other fields here ... | ||
+ | |||
+ | {{ form_end(form) }} | ||
+ | </ | ||
+ | |||
+ | Here is my code in my form object: | ||
+ | |||
+ | <code php> | ||
+ | $builder-> | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | )); | ||
+ | </ | ||
+ | |||
+ | Add here is my output: | ||
+ | |||
+ | <code html> | ||
+ | <!-- This is the one I need, I removed the prototype for the sake of clarity --> | ||
+ | <div id=" | ||
+ | <h3> | ||
+ | <label class=" | ||
+ | </h3> | ||
+ | <a href="#" | ||
+ | </ | ||
+ | <!-- This one is automatically added. The prototype is empty --> | ||
+ | <div> | ||
+ | <label class=" | ||
+ | <div id=" | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Do you have any ideas why this happens? | ||
+ | |||
+ | I followed that [[http:// | ||
+ | |||
+ | **Edit**: | ||
+ | |||
+ | This happens only when my collection is empty | ||
+ | |||
+ | <WRAP help> | ||
+ | I found the solution to that. | ||
+ | |||
+ | It turns out that when the collection is empty, the rendering of it never occurs. I could use **sjagr** solution but as there is some fields I want to render automatically, | ||
+ | |||
+ | So my solution is the following: | ||
+ | |||
+ | <code twig> | ||
+ | {{ form_errors(form) }} | ||
+ | {{ form_start(form) }} | ||
+ | |||
+ | ... some fields here ... | ||
+ | |||
+ | <div id="{{ form.items.vars.id }}" class=" | ||
+ | < | ||
+ | <a href="#" | ||
+ | {% for item in form.items %} | ||
+ | {{ form_row(item) }} | ||
+ | {% else %} | ||
+ | {% do form.items.setRendered %} | ||
+ | {% endfor %} | ||
+ | </ | ||
+ | |||
+ | ... some other fields here ... | ||
+ | |||
+ | {{ form_end(form) }} | ||
+ | </ | ||
+ | </ | ||
+ | <WRAP help> | ||
+ | When your collection is empty, the '' | ||
+ | |||
+ | Then you do '' | ||
+ | |||
+ | > This helper also outputs '' | ||
+ | |||
+ | So, you must simply pass a '' | ||
+ | |||
+ | <code twig> | ||
+ | {{ form_end(form, | ||
+ | </ | ||
+ | |||
+ | I'm not sure how '' | ||
+ | |||
+ | As an aside, you should consider making your own '' | ||
+ | </ | ||
+ | <WRAP help> | ||
+ | Unfortunately i cannot comment yet (no 50 rep), but perhaps this is caused the browser itself. A rendering issue due to markup someplace? See if the raw http response has the same thing. Since it works with form_row(form.items) but not your own, it "could be" that. | ||
+ | |||
+ | I would check if it's empty also before outputting it. | ||
+ | </ | ||
+ | <WRAP info> | ||
+ | [[https:// | ||
+ | </ | ||