相关疑难解决方法(0)

自定义身份验证 - 登录Symfony2消息

所以我正在阅读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)

customization login messages symfony

19
推荐指数
2
解决办法
2万
查看次数

翻译登录表单的错误消息

我使用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 …

translation symfony

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

symfony ×2

customization ×1

login ×1

messages ×1

translation ×1