Symfony& FOSUserBundle.自定义注册模板上的验证错误 [英] Symfony & FOSUserBundle. Customize validation errors on register template

查看:66
本文介绍了Symfony& FOSUserBundle.自定义注册模板上的验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图解决我的问题:我找不到在哪里定义register_content.html.twig的验证错误.

I was trying to solve my problem: I'm not able to find where the validation errors of register_content.html.twig are defined.

我需要知道如何显示我想要的错误.如今我的注册器看起来像:

I need to know how to show that errors as I would like. Nowadays my Register is looks like:

如您所见,存在一个难看的验证错误.我想更改标签

As you can see, there is a ugly validation error. I want to change the tags

<div>
 <ul><li> Las dos contraseñas no coinciden. </li></ul> 
</div>

在其他地方预先定义.

我已覆盖视图 register_content.html.twig ,我可以显示该验证错误,但不能对其进行自定义.

I have overridden the view register_content.html.twig and I can to show that validation errors but not customize it.

<div>
    {{ form_errors(form.email) }}
    {{ form_errors(form.username) }}
    {{ form_errors(form.plainPassword.first) }}
    {{ form_errors(form.plainPassword.second) }}
</div>

您知道我如何自定义验证错误的HTML结构吗?

Do you know how I can customize HTML structure of the validation errors?

例如,在login_content.html.twig中,验证错误如下所示:

For example in login_content.html.twig the validation errors are shown like this:

这是可能的,因为在login_content.html.twig中定义了验证错误:

It is possible because in login_content.html.twig are defined the validation errors:

{% if error %}
    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

推荐答案

我找到了解决方案.我将Twig中的{{ form_error }}变量更改为form.field.vars.errors.

I found the solution. I have change the {{ form_error }} variable in Twig for form.field.vars.errors.

例如,如果您要自定义电子邮件验证错误:

An example, If you want to customize your email validation errors:

<div>
    {{ form_errors(form.email) }}
</div>

通过以下方式进行更改:

change it by:

{% for errorItem in form.email.vars.errors %}
    <div>{{ errorItem.message }}</div>
{% endfor %}

您可以使用 div 标记或所需的任何标记.

You could use a div tag or whatever tag you want.

这篇关于Symfony&amp; FOSUserBundle.自定义注册模板上的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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