在@UniqueEntity 消息中添加 link

add link in @UniqueEntity message

您好,我正在使用 symfony 5.4,并尝试将 html 代码添加到 uniqueentity 消息中:

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @ORM\Table(indexes={@ORM\Index(name="user",columns={"id","credential","nickname","email","status"})})
 * @UniqueEntity(
 *     fields={"email"},
 *     message="este Correo ya esta en Uso; Dirigete a la Activacion de Cuentas! <a href='/account_activation'>Activate</a>"
 * )
 */
class User implements UserInterface, PasswordAuthenticatedUserInterface

在这种情况下,我希望在错误中显示 link,但我不明白:

这是树枝模板:

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

{% block body %}
    {# display any flash message #}
    {% for label, messages in app.flashes %}
        {% for message in messages %}
            <div class="alert alert-{{ label }} alert-dismissible fade show">
                {{ message }}
            </div>
        {% endfor %}
    {% endfor %}
    {{ form(registration_form) }}
{% endblock %}

知道如何实现吗?

更新

我尝试添加原始错误过滤器:

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

{% block body %}
    {{ error.message|raw }}
    {% for label, messages in app.flashes %}
        {% for message in messages %}
            <div class="alert alert-{{ label }} alert-dismissible fade show">
                {{ message }}
            </div>
        {% endfor %}
    {% endfor %}
    {{ form(registration_form) }}
{% endblock %}

但出现此错误:

如何在正面显示?如果你使用 Twig,也许你忘记使用 raw 过滤器?

   {{ error.message|raw }}

经过两周的研究:

使用注释中使用uniqueentity的消息属性,如果您想实现任何element/structure html.[=,这不是正确的方法(是一种不好的做法) 15=]

所有属性都经过编码以避免 xss 攻击,使用控制器有 2 种方法:

  1. 忘记 html 标签并使用直接重定向到所需路径而不是 link:
if (count($form['email']->getErrors(true)) > 0) {
    return $this->redirectToRoute('account_activation');
}
  1. 实施自定义错误处理程序并显示连接到 html 标记的错误:
if (count($form['email']->getErrors(true)) > 0) {
    $this->addFlash(
        'warning',
        'your account need to be activated! <a href="account_activation">Click Here!</a>'
    );
}

注意: 实施第二个选项需要将 Flash Messages 输出添加到表单模板。

github 站点提供了另一种未经验证的方法,但我无法验证其功能:

https://github.com/symfony/symfony/issues/44755#issuecomment-999156755

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

{% form_theme registration_form _self %}
{% block form_errors %}
    {%- for error in errors -%}
        {{ error.message | raw }}
    {%- endfor -%}
{% endblock %}

{% block body %}
    {{ form_errors(registration_form) }}
    {{ form(registration_form) }}
{% endblock %}