渲染 Twig 模板时,我在 Controller 中设置一条闪现消息。我正在检查用户电子邮件是否存在于数据库中,闪存将为绿色,否则为红色。 我想在 5 秒后清除消息闪烁,但不知道如何。
Twig .html
{#=======================( )===========================#}
{#====================( )========================#}
{#=================( flash messages )=====================#}
{#====================( )========================#}
{#=======================( )===========================#}
{% for message in app.flashes('notice') %}
{% if var %}
<div class="flash-notice" style="background-color:red;">
{{ message }}
</div>
{% else %}
<div class="flash-notice" style="background-color:green;">
{{ message }}
</div>
{% endif %}
{% endfor %}
Controller .php
/**
* @Route("/find_email", name="find_email")
*/
public function FindEmail(Request $request,EventDispatcherInterface $eventDispatcher)
{
$user = new User();
$email = $request->request->get("inputEmail", "valeur par défaut si le champ n'existe pas");
$user = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(['email' => $email]);
////////////////////////////////////
if (!$user) {
$var=true;
$this->addFlash("notice", "This is an error message");
} else {
$user_=$user;
$var=false;
$this->addFlash("notice", "This is a success message");
}
return $this->render('Security/reset_password.html.twig',[
'form'=> $email,
'var'=>$var,
]);
}
最佳答案
您需要编写一个 JavaScript 函数,在页面加载五秒后删除 .flash-notice
。
关于javascript - symfony 4 - 如何清除 5 秒后的 Flash 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52940336/