我想覆盖form_widget_simple功能
{% block form_widget_simple %}
{% spaceless %}
{% set type = type|default('text') %}
{% if errors|length > 0 %}
{{dump(form.vars.attr)}}
{% endif %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endspaceless %}
{% endblock form_widget_simple %}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何设置form.vars.attr['class']内部if声明当我这样做set form.vars.attr['class'] = 'error';我得到错误Unexpected token "punctuation" of value "." ("end of statement block" expected)
如您所见,在widget_attributes块中处理添加其他属性.如果你看一下那里,你会看到一个简单的foreach在attr数组上,具有所有属性.我认为可以完成一个合并现有的简单集合.所以你的form_widget_simple块看起来像
{% block form_widget_simple %}
{% spaceless %}
{% set type = type|default('text') %}
{% if errors|length > 0 %}
{{dump(form.vars.attr)}}
{% endif %}
{% set attr = attr|merge({'class': (attr.class|default('') ~ ' your-css-class')|trim}) %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endspaceless %}
{% endblock form_widget_simple %}
Run Code Online (Sandbox Code Playgroud)
这将保留表单构建器中设置的每个类属性,并添加your-css-class为附加类.如果未定义类属性,则仅your-css-class设置.
| 归档时间: |
|
| 查看次数: |
2569 次 |
| 最近记录: |