Customize field name in validation error messages

Asked

Viewed 3,733 times

3

I’m using the Laravel 5.2 Standard Authentication Controller:

php artisan make:auth

As the names are in English, I need to change to Portuguese. I have already set the locale to Portuguese, in /config/app.php:

'locale' => 'pt-BR',

I changed the Authcontroller.php as follows:

protected function validator(array $data)
{
    $validator = Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);        
    $correct_names = [
        'name'      => 'Nome',
        'email'     => 'E-mail',
        'password'  => 'Senha',
    ];

    return $validator->setAttributeNames($correct_names);
}

When creating a user, using http://localhost:8000/Register, the field names are correct, however on the login page, http://localhost:8000/login, the name of the fields continue in English (email and password).

Can someone give a hint how to solve this?

3 answers

5


Another way to customize the field name is directly in your language file by changing the key attributes

/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap attribute place-holders
| with something more reader friendly such as E-Mail Address instead
| of "email". This simply helps us make messages a little cleaner.
|
*/

'attributes' => [
    'email'    => 'E-mail',
    'password' => 'Senha',
],

This way all error forms containing these attributes will be renamed.

4

The code that implements the user validation method (function) both in the login when in the creating of new users, can be overwritten in the file.

The method of validation of the login is validateLogin(), located in:

/vendor/Laravel/framework/src/Illuminate/Foundation/Auth/Authenticatesusers.php

To replace this method, enter this same function in the file:

/app/Http/Controllers/Auth/Authcontroller.php

With the necessary amendments, in the case in question:

protected function validateLogin(Request $data)
{
    $this->validate($data, [
            $this->loginUsername()  => 'required', 
            'password'              => 'required'
        ],[],[
            $this->loginUsername()  => 'E-mail',
            'password'              => 'Senha',
        ]
    );
}

I hope I’ve helped!

3

This customization is quite simple, you will only need to implement one more array to introduce custom error messages, for example:

protected function validator(array $data)
{
    $validator = Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ], [
        'required' => 'O campo :attribute é obrigatório',
    ], [
        'name'      => 'Nome',
        'email'     => 'E-mail',
        'password'  => 'Senha',
    ]);        

    return $validator;
}
  • The form described above is identical to the one I have in the question, but works only on the form Register and not on the form login, when, for example, you leave the E-mail and Password field blank and press 'Login', the field name will appear as the original of the system.

  • I know if I change the function validateLogin in the archive Autheticatesusers.php, in Illuminate Foundation Auth and put the way you posted: $this->validate($request, [$this->loginUsername() =>'required','password' => 'required'], ['required' => 'The field :attribute is required! '], [$this->loginUsername() => 'User','password'=> 'Password',]); will work.. but I don’t think this is the best way out by changing the internal code of the Laravel.... =(

Browser other questions tagged

You are not signed in. Login or sign up in order to post.