本文介绍了为错误渲染定义自定义树枝表单块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为表单字段错误呈现定义一个特定的新块,保持 form_errors 不变以呈现常见错误.
# Twig 配置枝条:调试:%kernel.debug%严格变量:%kernel.debug%形式:资源:- 'ApplicationMyBundle:Main:form/customFormTheme.html.twig'
在 customFormTheme.html.twig
中,我覆盖了从 form_div_layout.html.twig
复制的几个块,并添加了以下新块.
{% block field_errors %}{% spaceless %}{% if errors|length >0%}<ul class="errors">{% for error in error %}{% if error.messageTemplate|length %}<li class="error">{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li>{% 万一 %}{% 结束为 %}{% 万一 %}{% endspaceless %}{% endblock %}
然后我希望能够像这样在我的视图中使用这个块:
{{ form_label(form.message, 'message.label'|trans({},'contact')|raw ) }}{{ form_widget(form.message, {attr: {maxlength:1000, size:1000, rows:8}}) }}{{ field_errors(form.message) }}
但我收到以下错误:
函数field_errors"不存在.您的意思是form_errors"吗
我也尝试通过命名我的块 text_errors
或 textarea_errors
提到 这里 但我没有那么幸运.
有什么想法吗?
解决方案
实际上它的工作原理是定义块 text_errors
或 textarea_errors
并且仍然使用 {{form_errors(field.name) }}
在您的模板中.如果存在以您的字段类型命名的块(根据表单字段类型)它将被用来代替form_errors
.
!!但是你不能在你的树枝模板中直接使用 {{ text_errors(field.name) }}
!!
同样的方式,你可以为这样的特定类型创建一个自定义行
{% block textarea_row %}{% spaceless %}<div class="textarea l-field {{ (form_errors(form)?'error':'') }}">{{ form_label(form) }}{{ form_widget(form) }}{{ form_errors(form) }}
{% endspaceless %}{% endblock textarea_row %}
并在您的模板中使用它,如下所示:
{# message has textarea field type #}{{ form_row(form.message, {标签:'message.label'|trans({},'contact')|raw ,属性:{最大长度:1000,大小:1000,行:8}})}}
您还可以使用对象 attr{}
传递许多自定义参数
{% 块 form_row %}{% 无空间 %}<div class="form-field {{ (form_errors(form)?'error':'') }}">{{ form_label(form) }}{{ form_widget(form) }}{{ 转储(属性)}}{% 如果定义了 attr.help 而不是 attr.help == '' %}<p class="form-help">{{ attr.help }}</p>{% endif %}{{ form_errors(form) }}
{% endspaceless %}{% endblock form_row %}
并像这样使用它
{{ form_row(form.message, {标签:'message.label'|trans({},'contact')|raw ,属性:{最大长度:1000,大小:1000,行:8,帮助: 'password.help'|trans({})|raw}})}}
I'm trying to define a specific new block for form field errors rendering, keeping form_errors unchanged for common errors rendering.
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
form:
resources:
- 'ApplicationMyBundle:Main:form/customFormTheme.html.twig'
In customFormTheme.html.twig
I overwrite a few blocks copied from form_div_layout.html.twig
plus I added the folloowing new one.
{% block field_errors %}{% spaceless %}
{% if errors|length > 0 %}
<ul class="errors">
{% for error in errors %}
{% if error.messageTemplate|length %}
<li class="error">{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
{% endspaceless %}{% endblock %}
Then I expect to be able to use this block in my views like this :
<div>
{{ form_label(form.message, 'message.label'|trans({},'contact')|raw ) }}
{{ form_widget(form.message, {attr: {maxlength:1000, size:1000, rows:8}}) }}
{{ field_errors(form.message) }}
</div>
but I receive the following error :
The function "field_errors" does not exist. Did you mean "form_errors"
I also tried by naming my block text_errors
or textarea_errors
mentioned here but I haven't been luckier.
Any idea ?
解决方案
Actually it works by defining the block text_errors
or textarea_errors
only and still use {{ form_errors(field.name) }}
in your template. If a block named after the type of your field exists (according to form field types) it will be used instead of form_errors
.
!! But you can't use directly {{ text_errors(field.name) }}
in your twig template !!
The same way you can have a custom row for a specific type like this
{% block textarea_row %}{% spaceless %}
<div class="textarea l-field {{ (form_errors(form)?'error':'') }}">
{{ form_label(form) }}
{{ form_widget(form) }}
{{ form_errors(form) }}
</div>
{% endspaceless %}{% endblock textarea_row %}
and use it in your template as follow :
{# message has textarea field type #}
{{ form_row(form.message, {
label: 'message.label'|trans({},'contact')|raw ,
attr: {maxlength:1000, size:1000, rows:8}})
}}
You can also pass many custom parameters by using the object attr{}
{% block form_row %}
{% spaceless %}
<div class="form-field {{ (form_errors(form)?'error':'') }}">
{{ form_label(form) }}
{{ form_widget(form) }}
{{ dump(attr) }}
{% if attr.help is defined and not attr.help == '' %}<p class="form-help">{{ attr.help }}</p>{% endif %}
{{ form_errors(form) }}
</div>
{% endspaceless %}
{% endblock form_row %}
and use it like this
{{ form_row(form.message, {
label: 'message.label'|trans({},'contact')|raw ,
attr: {
maxlength:1000, size:1000, rows:8,
help: 'password.help'|trans({})|raw
}
})
}}
这篇关于为错误渲染定义自定义树枝表单块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!