Customize field name in validation error messages

3

I am using the Laravel 5.2 authentication standard Controlller:

php artisan make:auth

As the names are in English, I need to change to Portuguese. I already configured the locale for 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 link , the field names are correct, however on the login page , link , the field names are still in English and email password ).

Can anyone give a hint how to solve this?

    
asked by anonymous 07.03.2016 / 18:55

3 answers

4

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',
],

In this way all error forms that contain these attributes will be renamed.

    
20.03.2016 / 14:49
3

This customization is very simple, you just need to implement one more array to enter 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;
}
    
07.03.2016 / 20:23
3

The code that implements the user validation (function) method, both in login and creation of new users, can be substituted in the file. >

The login validation method is validateLogin () , located at:

  

/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

To replace this method, insert the same function into the file:

  

/app/Http/Controllers/Auth/AuthController.php

With the necessary changes, 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 have helped!

    
11.03.2016 / 12:51