Symfony2自定义表单错误消息 [英] Symfony2 customize form error messages

查看:82
本文介绍了Symfony2自定义表单错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您帮我了解如何使用Symfony2在表单中自定义错误消息? 我想更改HTML布局,添加div,class等...

can you please help me understand how to customize error messages in a form with Symfony2? I want to change the HTML layout, adding div, class, etc...

阅读该指南后,它给出了一段代码放置到名为fields_errors.html.twig的文件中,但没有告知该文件的放置位置以及是否需要一些额外的配置.

Reading the guide, it gives a piece of code to put in a file called fields_errors.html.twig but it doesn't tells where to put this file and if some extra configuration is needed.

有人可以帮我吗?

推荐答案

您必须将模板放在包的Resourses/views/文件夹中.例如,

You have to put the template in Resourses/views/ folder of your bundle. For example,

{# Vendor/YourBundle/Resourses/views/form_fields.html.twig #}

{% extends 'form_div_layout.html.twig' %}

{% block form_errors %}
    {# your form error template #}
{% endblock form_errors %}

{# other customized blocks #}

然后在表单页面模板中,

And then in your form page template,

{% extends your:page:layout %}

{% form_theme form  'VendorYourBundle::form_fields.html.twig' %}

 {{ form_errors(form.field) }}
 {# ..... #}

有关更多选项和实现的参考,请参见表单主题菜谱条目

For more option and implementation reference check form theme cookbook entry and default field layout implementation

这篇关于Symfony2自定义表单错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆