Twig - Iterate over form fields

I ran into a problem trying to iterate over all form elements in a Twig form theme. In fact, I used this to iterate over them:

{% for child in form.parent.children %}

I did not find it in the documentation, I just reset the form of the variable and found the fields along the way. This really works well with all forms, unless the form itself has a parameter called child. I do not know if this is an error, because if you are just a dump formobject, then the structure is the same in both cases:

enter image description here

But if you try to access form.parent.childrenwhen it has a parameter childreninside, you will not get this array more likely directly from the result "children":

enter image description here

, form.parent.children child ( # 1592). , form.parent.children.parent.children, children, , children.

, - ? , , ?

+4
1

, , , Twig , FormView \ArrayAccess . , parent vars, , .

Twig, . , FormView:

public function getFunctions()
{
    return array(
        new TwigFunction('formview_prop', array($this, 'getFormViewProperty')),
    );
}

public function getFormViewProperty(FormView $formView, string $prop)
{
    // parent, children or vars
    return $formView->{$prop};
}

, , ( ):

{% for child in formview_prop(form, 'parent') %}

( ) . , formview_parent, formview_children formview_vars .


, , Symfony 2.7.39, 2.8.32, 3.3.14, 3.4.1, 4.0.1 ( ) Twig rootform, , parent:

{% if form is rootform %}
+3

Source: https://habr.com/ru/post/1690953/


All Articles