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

本文是小编为大家收集整理的关于Symfony & FOSUserBundle。自定义注册模板上的验证错误的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

我需要知道如何按照我的意愿显示错误.如今,我的登记册看起来像:

在此处输入图像说明

您可以看到,存在一个丑陋的验证错误.我想更改标签

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

由另一个地方的预定义的地方.

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

<div>
    {{ 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>
{% endif %}

推荐答案

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

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

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

通过:

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

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

其他推荐答案

您将必须覆盖默认的符号符号错误块. 您可以在symfony 文档

您将能够设置此表单主题全球或每个视图,如文档

例如
{% form_theme form _self %}

{# form_errors.html.twig #}
{% block form_errors %}
    {% spaceless %}
        {% if errors|length > 0 %}
        <div class='alert alert-danger'>
            {% for error in errors %}
                <div>{{ error.message }}</div>
            {% endfor %}
        </div>
        {% endif %}
    {% endspaceless %}
{% endblock form_errors %}

并仅在注册表格中使用此自定义主题,您将在Override文件的顶部拨打register_content.html.twig

{% form_theme form 'Form/form_errors.html.twig' %}

其他推荐答案

您可以使用 translation . in Parameters.ini 设置语言环境创建消息文件.然后在树枝模板中使用:

    {% if error %}
        <div class="error">{{ error.message|trans({},'messages') }}</div>
    {% endif %}

另一个解决方案是:

{{ error.message | replace({"Bad credentials." : "Invalid username or password."}) }}

本文地址:https://www.itbaoku.cn/post/2090795.html

问题描述

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:

enter image description here

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:

enter image description here

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.

其他推荐答案

You will have to override the default symfony form error block. You can find out how to do it in the symfony documentation

you will be able to set this form theme globaly or per view as shown in the documentation

So for example if you would like to display the errors inside a div with a class alert alert-danger you would create a file views/Form/form_errors.html.twig

{% form_theme form _self %}

{# form_errors.html.twig #}
{% block form_errors %}
    {% spaceless %}
        {% if errors|length > 0 %}
        <div class='alert alert-danger'>
            {% for error in errors %}
                <div>{{ error.message }}</div>
            {% endfor %}
        </div>
        {% endif %}
    {% endspaceless %}
{% endblock form_errors %}

and to use this custom theme just in the registration form you would call at the top of your override file register_content.html.twig

{% form_theme form 'Form/form_errors.html.twig' %}

其他推荐答案

You can use translation. In parameters.ini set locale to your language and create message file. Then in twig template use:

    {% if error %}
        <div class="error">{{ error.message|trans({},'messages') }}</div>
    {% endif %}

Another solution would be:

{{ error.message | replace({"Bad credentials." : "Invalid username or password."}) }}