Alex.Barylski
Alex.Barylski

Reputation: 2953

Symfony 5+ nested form collection rendering

{% form_theme form _self %}
{% block collection_entry_row %}

    {% set fields = form.attributes.children[0].children %}
    {% for key, field in fields %}
        {{ form_widget(field) }}
        {{ form_widget(fields[key]) }}
    {% endfor %}

{% endblock %}

And I get this error:

Neither the property "attributes" nor one of the methods "attributes()", "getattributes()"/"isattributes()"/"hasattributes()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".

But this

{% set fields = form.attributes.children[0] %}
{% for key, field in fields %}
    {{ dump(field) }}
{% endfor %}

Produces this:

Symfony\Component\Form\FormView {#2150 ▼
  +vars: array:33 [▶]
  +parent: Symfony\Component\Form\FormView {#2203 ▶}
  +children: []
  -rendered: false
  -methodRendered: false
}

And yet inside the form_start() I can do the following and render one field at a time:

{% set tempInput = form.children['attributes'].children[0].children['attributes'].children[0].children['pin'] %}
{{ form_widget(tempInput) }}

Thoughts?

Upvotes: 0

Views: 612

Answers (1)

mickael lebihan
mickael lebihan

Reputation: 21

just had the same questions as you about this subject, and since i didn't understands the way to use the "{% block collection_row %}" i've found this following workaround.

I find out that the problem i had was coming from the 2 last item of the form.children: "submit" and "_token". Because, of course, they will not have the variable name you are searching in the other "real" forms childs.

So i just added a check to the length of the children array to exclude this "submit" and "_token" items. (The test might certainly be different for your case).

{% for child in form.children %}
  {% if not child.children | length != 4  %} // my array has 4 items, submit and _token have none

    {{ form_widget(child.children.variableName) }}

  {% endif %}
{% endfor %}

As you can see you can access your collection variable between the if like this:

{{ form_widget(child.children.variableName) }}

Peace.

Upvotes: 1

Related Questions