对于项目需求,我需要在渲染之前更改一些表单字段数据 . 为此,我迭代表单元素并动态更改我需要的值 .
问题是我无法改变 Value . 我收到了这个错误:
Unexpected token "punctuation" of value "." ("end of statement block" expected).
我使用这段代码来改变值,但我得到了错误:
{% set arr = arr|merge({'element': 'value'}) %}
有谁知道问题出在哪里?
这是我用过的代码 .
Twig模板代码(我使用的示例代码)
<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>
表单对象:
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
}
1 回答
要改变数组的某个索引,你最好扩展
Twig
,解决方案可能就是这个,ProjectTwigExtension.php
添加扩展到树枝
template.twig