自定义身份验证-登录Symfony2消息 [英] Customize Authentication - Login Symfony2 Messages

查看:122
本文介绍了自定义身份验证-登录Symfony2消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在阅读Symfony2 Book的安全性一章.我了解所有内容,但是如果出现登录错误,我想自定义错误消息.

So I'm reading the security chapter of Symfony2 Book. I understand everything, but I'd like to customize the error message if a there is a login error.

我可以在哪个文件中进行更改?

In which file can I change this?

这是模板:

{% if error %}
    <div>{{ error.message }}</div>
{% endif %}

<form action="{{ path('login_check') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />

<label for="password">Password:</label>
<input type="password" id="password" name="_password" />

{#
    If you want to control the URL the user is redirected to on success (more details below)
    <input type="hidden" name="_target_path" value="/account" />
#}

<input type="submit" name="login" />

我认为最糟糕的方法是:

I believe the worst way of doing this would be something like:

if (error.message=="Bad credentials")
    echo "Los datos son erróneos :)"

if (error.message==The presented password is invalid")
    echo "La combinación username/password no es correcta :)"

你能帮我吗?

,我可以正常运行:

万一有人需要这样做,请确保将这行添加到config.yml

In case someone needs to do this, be sure you add this line to the config.yml

#app/config/config.yml
framework:
    translator: { fallback: en }

并在文件中放入messages.whateverisyourlanguage.yml,在我的情况下是messages.es.yml,如下所示:

and put in the file messages.whateverisyourlanguage.yml, in my case messages.es.yml, lines like this one:

您要翻译的文本:翻译后的文本

Text you want to translate : Translated text

#Foo\DummyBundle\Resources\translations\messages.es.yml
The presented password cannot be empty.: El campo contrasena no debe estar vacio
The presented password is invalid.: Los datos suministrados son incorrectos
Bad credentials: Los datos suministrados son incorrectos

请谨慎选择要翻译的文字.如果文本的末尾有一个圆点,则必须放置该圆点.我没有这样做,也没有用.

Be careful with the text you want to translate. If the text has a dot at the end, you have to put the dot. I wasn't doing that and it wasn't working.

footranslate.footranslate

问候! :)

推荐答案

您可以使用翻译.在parameters.ini 设置语言环境到您的语言并创建

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

这篇关于自定义身份验证-登录Symfony2消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆