【问题标题】:在@UniqueEntity 消息中添加链接
【发布时间】:2022-01-22 15:01:06
【问题描述】:

嗨,我正在使用 symfony 5.4,并尝试将 html 代码添加到唯一实体消息:

/**
 * @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

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

这是树枝模板:

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

但得到这个错误:

【问题讨论】:

    标签: symfony orm


    【解决方案1】:

    如何将其显示在正面?如果您使用 Twig,可能您忘记使用raw 过滤器?

       {{ error.message|raw }}
    

    【讨论】:

    • 你好,我没有添加任何错误输出,在 tampleate 错误中我认为 flash 是 showqing 或 symfony 在输入附近显示它。我已经用树枝模板内容更新了问题。
    • 尝试:{{ form_errors(registration_form.email)|raw }}
    • 好的,我已将它添加到 flash 的 uppfer 中……我得到了两次……链接(锚点)不起作用:i.imgur.com/yoo0k8e.png
    • 我已经在github上公开了github.com/symfony/symfony/issues/44755
    • 经过一些测试后你的解决方法不起作用......
    【解决方案2】:

    经过两周的研究:

    在注解中使用使用唯一实体的消息属性,如果你想实现任何元素/结构的html,这不是正确的方法(是一种不好的做法)。

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

    1. 忘记 html 标记,使用直接重定向到所需路径而不是链接:
    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 %}
    

    【讨论】:

      猜你喜欢
      • 2010-10-23
      • 2019-02-28
      • 2015-06-05
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      相关资源
      最近更新 更多