问题描述
所以我正在阅读 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
#FooDummyBundleResources ranslationsmessages.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 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!