Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnk's avatar
Level 1

Laravel auth with custom error messages

I'm using the Laravel Auth but I would like to have custom error messages:

$rules = [
    'email' => 'required|email|exists:users.email',
    'password' => 'required',
];

$messages = [
    'email.required' => 'The email is required.',
    'email.email' => 'The email needs to have a valid format.',
    'email.exists' => 'The email is not registered in the system.',
];


$this->validate($request, $rules, $messages);

Do you know where to do this custom messages configuration?

0 likes
13 replies
johnk's avatar
Level 1

Thanks, I'm using "de" language in app.php not "en".

Sergiu17's avatar

@johnk create a folder resources/lang/de/auth.php

<?php

return [
    'failed' => 'Failed message in DE',

And make sure that in config file config/app.php you set your locale to de

'locale' => 'de',
1 like
johnk's avatar
Level 1

But so, with that approach is only possible to have 1 equal error message for all errors?

Sergiu17's avatar

Yes, you could do something like this

$validator = Validator::make($request->all(), [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
]);

if ($validator->fails()) {
    return redirect('post/create')
    ->withError('My error message');
}

https://laravel.com/docs/5.6/validation

1 like
Cronix's avatar

If you're looking to change the error messages when they register, it's just in /app/Http/Controllers/RegisterController.

protected function validator(array $data)
{
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
}

You can add the custom messages by passing another array to Validator as the 3rd parameter, like you were doing in your initial post.

protected function validator(array $data)
{
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ], [
            'email.required' => 'The email is required.',
            'email.email' => 'The email needs to have a valid format.',
            'email.exists' => 'The email is not registered in the system.',
       ]);
}

Edit: your mail.exists message is the opposite of what it should be though. "The email is ALREADY registered in the system"

1 like
johnk's avatar
Level 1

Ok, so to change the messages when the user registers it's in the RegisterController, so to change the messages when the user logs in it's necessary to add the validator() to the LoginController?

Cronix's avatar
Cronix
Best Answer
Level 67

The LoginController only uses the 2 messages found in the translation file resources/lang/en/auth.php

'failed' => 'These credentials do not match our records.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',

So you'd need to change it there, or copy the file to resources/lang/de/auth.php and translate them to German.

If you look at the AuthController, you'll see it uses the AuthenticatesUsers trait. If you look in that trait, you'll see

protected function sendFailedLoginResponse(Request $request)
{
    throw ValidationException::withMessages([
        $this->username() => [trans('auth.failed')],
    ]);
}

which is where it's loading the auth.failed message from the translation file ("These credentials do not match our records.")

6 likes
johnk's avatar
Level 1

Thanks, but with that approach (add resources/lang/de/auth.php file) and change the message text is only possible to have only 1 message that is equal for all errors. But to have different messages, one for the case that the user doesn't fill the form field, other for the case of the email don´t have a valid format, etc, it's necessary to add in the LoginController the validator()?

Cronix's avatar

message text is only possible to have only 1 message that is equal for all errors

No. The login controller only has one message to change (unless you're doing throttling too, in which case there are only 2 to change). LIke if they enter the wrong email or password, so there's nothing else to change there. This is completely separate from when they are registering

The other place to change is in RegistrationController, like I already showed where you can change the errors individually (name, email.required, email.exists, password errors, etc). I'm not sure why you're getting confused.

  1. Errors for registering (in RegistrationController)
  2. Error for logging in (in language file)
1 like
Snapey's avatar

AuthenticatesUsers trait only checks that the username is present and a string and that the password is present.

There isn't a check for username being a valid email address

    protected function validateLogin(Request $request)
    {
        $this->validate($request, [
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }

You could override the function locally if you want to check email format.

These two fields being missing will return the standard error messages for required fields.

1 like
saadaan's avatar

Hi,

I implemented the SUSPENDED user logic in LoginController, and need to show the appropriate error message when it is triggered. How are these 'failed' and 'throttle' keywords triggered? How can I put in my own customer error message with a trigger keyword?

Thanks! Saad

Please or to participate in this conversation.