Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - How to translate "Bad credentials" login error message from FOSUserBundle ?

I'd like to customize the error message ('Bad credentials')if a there is a login error. I do it but doesn't work. This is the template login :

{% if error %}
<div>{{ error.message|trans }}</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" />
<input type="hidden" name="_target_path" value="/account" />
<input type="submit" name="login" />

the file config.yml I have this :

framework:
#esi:             ~
translator:      { fallback: "%locale%" }

in parametre.yml I have this:

locale: en

I added the file in directory MyBundle/Resources/translations/messages.en.ymy:

security:
login:
    username: "Username:"
    password: "Password:"
    submit: Login
    forgot_username: "Forgot username"
    forgot_password: "Forgot password"
    registration: register
    # Security
    "Bad credentials": "Your user name or password are incorrect."

but always I have the Bad credentials error message

like image 727
mehdiraddadi Avatar asked May 10 '14 09:05

mehdiraddadi


2 Answers

The problem is that error message has dot(.) at the end of the string.

You can correct this using the twig trim filter:

{{ error|trim('.')|trans({}, 'FOSUserBundle') }}
like image 82
Rafał Rudnicki Avatar answered Oct 07 '22 18:10

Rafał Rudnicki


In my login form I have :

{# Error login form #}
{% if error %}
    <div class="alert alert-danger">
        <strong>{% trans %}login.error{% endtrans %}!</strong> {{ error.messageKey|trans({}, 'messages') }}
        </div>
{% endif %}

Here the 'messages' is important as it refers the domain of your translation, or if you prefer the name of your translation file. In my case : messages.en.yml

With the fosuserbundle 1.3.5 you have to put this in your message.yml

"Invalid credentials." : Bad email/password combination

I hope this will help

like image 26
0x1gene Avatar answered Oct 07 '22 17:10

0x1gene