Laravel 5.2 - Auth: display custom error messages

How to configure error messages (for example, "These credentials do not match our records." ) That appear when registration / registration fails without having to touch the foundation files? I am looking for a solution and hopefully elegant, at least not touching AuthenticatesAndRegistersUsers and ThrottlesLogins :)

I use AuthController and the forms provided by Laravel after execution:

php artisan make: auth

Controller:

(it only has a constructor and two methods, the rest are based on methods)

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

the form:

 <form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}"> {!! csrf_field() !!} <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> <label class="col-md-4 control-label">E-Mail</label> <div class="col-md-6"> <input type="email" class="form-control" name="email" value="{{ old('email') }}" required> @if ($errors->has('email')) <span class="help-block"> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> </div> <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}"> <label class="col-md-4 control-label">Password</label> <div class="col-md-6"> <input type="password" class="form-control" name="password" required> @if ($errors->has('password')) <span class="help-block"> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> <div class="checkbox"> <label> <input type="checkbox" name="remember"> Remember Me </label> </div> </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> <button type="submit" class="btn btn-primary"> <i class="fa fa-btn fa-sign-in"></i>Login </button> <a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a> </div> </div> 

Thanks!

+7
authentication php error-handling
source share
2 answers

You can override getFailedLoginMessage on an AuthController that comes from AuthenticatesUsers trait

 protected function getFailedLoginMessage() { return 'what you want here.'; } 

Or do not override it and set the lang value to auth.failed . The getFailedLoginMessage method will check Lang::has('auth.failed') and use it if available.

For actual validation error messages, you can override postLogin and pass your own array of messages to validate , or if you want to change them globally, you can configure them in the corresponding lang file in resources/lang/{lang}/validation.php .

+11
source share

You do not want to override the getFailedLoginMessage () method in AuthController. The correct solution is to change the message at the planned location. If you look in the Resources> lang> en folder, you will see the auth.php file. It has the attribute "failed" with a message that you can configure. Change him. The orignial getFailedLoginMessage () method in the Laravel auth files in the provider first looks for this location for the custom message before setting the default value.

+4
source share

All Articles