所以我正在阅读Symfony2 Book的安全章节.我理解一切,但如果有登录错误,我想自定义错误消息.
我可以在哪个文件中更改此内容?
这是模板:
{% 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" />
Run Code Online (Sandbox Code Playgroud)
我相信这样做的最糟糕方式是:
if (error.message=="Bad credentials")
echo "Los datos son erróneos :)"
if (error.message==The presented password is invalid") …Run Code Online (Sandbox Code Playgroud) 我使用FOSUserBundle登录表单,我想翻译错误消息.这些消息在这里发布:vendor/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php
protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
{
$currentUser = $token->getUser();
if ($currentUser instanceof UserInterface) {
if ($currentUser->getPassword() !== $user->getPassword()) {
throw new BadCredentialsException('The credentials were changed from another session.');
}
} else {
if (!$presentedPassword = $token->getCredentials()) {
throw new BadCredentialsException('The presented password cannot be empty.');
}
if (!$this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt())) {
throw new BadCredentialsException('The presented password is invalid.');
}
}
}
Run Code Online (Sandbox Code Playgroud)
我写了app/Resources/translations/validators.fr.yml
"The presented password cannot be empty.": "Veuillez saisir un mot de passe."
Run Code Online (Sandbox Code Playgroud)
我写了app/Resources/translations/messages.fr.yml …