Symfony &FOS 用户包.自定义注册模板上的验证错误

2022-01-22 00:00:00 php symfony twig fosuserbundle

我试图解决我的问题:我无法找到 register_content.html.twig 的验证错误的定义位置.

我需要知道如何显示我想要的错误.现在我的注册是这样的:

如您所见,有一个丑陋的验证错误.我想更改标签

<ul><li>Las dos contraseñas 并非巧合.</li></ul></div>

在某处由另一个预先定义.

我已经覆盖了视图 register_content.html.twig,我可以显示验证错误但不能自定义它.

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

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

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

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

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

解决方案

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

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

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

改变它:

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

您可以使用 div 标签或任何您想要的标签.

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>

predefined somewhere, by another.

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>

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

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

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 %}

解决方案

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 %}

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

相关文章