Reputation: 107
For ajax purpose, I want to access to the form prototype of a collection. I can get it if there is data in the collection. But if the collection is empty data-prototype return an empty string.
How I can return the prototype even if the collection is still empty?
The form field:
->add('colors', CollectionType::class, array(
'entry_type' => JnTestColorColorType::class,
'entry_options' => array('label' => false),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'required'=>false,
'prototype'=>true
))
The view:
{% if form.colors is defined %}
<h3>Colors</h3>
<ul class="colors" data-prototype="{{ form_widget(form.colors.vars.prototype)|e('html_attr') }}">
{# iterate over each existing tag and render its only field: name #}
{% for color in form.colors %}
<li>{{ form_row(color.color) }}</li>
{% endfor %}
</ul>
{% endif %}
Upvotes: 1
Views: 699
Reputation: 107
thanks to @Philippe-B- remark
Have you tried rendering the collection field all at once to see if that makes any difference ?
I need to render all field at once to get the prototype in my view!
Upvotes: 2
Reputation: 636
As mentionned in the documentation, there are two ways to render the data-prototype attribute :
form_row(form.emails)
).data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}"
).In both cases the prototype should be rendered regardless of the underlying data.
Upvotes: 0