嗨,我正在使用 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 %}
但得到這個錯誤:

uj5u.com熱心網友回復:
怎么顯示在前面?如果您使用 Twig,也許您忘記使用原始過濾器?
{{ error.message|raw }}
uj5u.com熱心網友回復:
經過兩周的研究:
使用在注釋中使用唯一性的訊息屬性,如果要實作任何元素/結構 html,這不是正確的方法(是一種不好的做法)。
所有屬性都經過編碼以避免 xss 攻擊,有兩種使用控制器的方法:
- 忘記 html 標簽并使用直接重定向到所需的路徑而不是鏈接:
if (count($form['email']->getErrors(true)) > 0) {
return $this->redirectToRoute('account_activation');
}
- 實作自定義錯誤處理程式并顯示連接到 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 %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/390474.html
