For project needs I need to change some form fields data before they render. To do that I iterate over form elements and dynamically change values I need.
Problem is that I can't change value. I got this error:
Unexpected token "punctuation" of value "." ("end of statement block" expected).
I used this piece of code to change value but I got error above:
{% set arr = arr|merge({'element': 'value'}) %}
Does someone knows where is the problem?
This is code I used.
Twig template code (example code I used)
<ul>
{% dump (edit_form) %}
{% for element in edit_form.children %}
{% dump (element.vars) %}
{% set element.vars = element.vars|merge({'name': 'My title (just for testing purpose)'}) %}
<li>{{ element.vars.name }}</li>
{% endfor %}
</ul>
Form object:
FormView {#637 ▼
+vars: array:24 [▶]
+parent: null
+children: array:4 [▼
"title" => FormView {#699 ▼
+vars: array:24 [▼
"value" => "le title"
"attr" => []
"form" => FormView {#699}
"id" => "adminbundle_intro_title"
"name" => "title"
"full_name" => "adminbundle_intro[title]"
"disabled" => false
"label" => null
"label_format" => "admin.intro.form.%name%"
"multipart" => false
"block_prefixes" => array:3 [▶]
"unique_block_prefix" => "_adminbundle_intro_title"
"translation_domain" => null
"cache_key" => "_adminbundle_intro_title_text"
"errors" => FormErrorIterator {#696 ▶}
"valid" => true
"data" => "le title"
"required" => true
"size" => null
"label_attr" => []
"compound" => false
"method" => "POST"
"action" => ""
"submitted" => false
]
+parent: FormView {#637}
+children: []
-rendered: false
}
"content" => FormView {#698 ▶}
"isEnabled" => FormView {#702 ▶}
"_token" => FormView {#711 ▶}
]
-rendered: false
}
To change a certain index of an array you're best of with extending
Twig
, a solution could be this,ProjectTwigExtension.php
Add extension to twig
template.twig