Reputation: 517
I am learning Laravel 5
and I have wrote following code for user login. Now I am trying display validation errors messages with this code:
@if(count($errors))
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
but nothing is displaying. :(
Any idea whyerrors are not displaying, If user not enter any value and hit submit button.
Thanks.
Route::get('/', function () {
return view('welcome');
});
Route::group(['middleware' => ['web']], function () {
Route::get('/login', ['as' => 'login', 'uses' => 'AuthController@login']);
Route::post('/handleLogin', ['as' => 'handleLogin', 'uses' => 'AuthController@handleLogin']);
Route::get('/home', ['middleware' => 'auth', 'as' => 'home', 'uses' => 'UserController@home']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'AuthController@logout']);
Route::resource('user', 'UserController', ['only' => ['create', 'store']]);
});
@extends('layouts.master')
@section('content')
<div class="row">
<div class="col-md-6">
<h2>Log in</h2>
<p>Hi, here you can login to your account.</p>
<br>
@if(count($errors))
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{!! Form::open(array('route' => 'handleLogin')) !!}
<div class="form-group">
{!! Form::label('email', 'E-Mail Address') !!}
{!! Form::text('email', null, array('class' => 'form-control','placeholder' => '[email protected]')) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', array('class' => 'form-control')) !!}
</div>
<div>
{!! Form::token() !!}
{!! Form::submit('Sign In' , array('class' => 'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
<br>
</div>
</div>
@endsection
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
class AuthController extends Controller
{
public function login()
{
return view('auth.login');
}
public function handleLogin(Request $request)
{
$this->validate($request, User::$login_validation_rules);
$data = $request->only('email', 'password');
if(\Auth::attempt($data)) {
return redirect()->intended('home');
}
return back()->withInput();
}
public function logout()
{
\Auth::logout();
return redirect()->route('login');
}
}
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public static $login_validation_rules = [
'email' => 'required|email|exists:users',
'password' => 'required'
];
}
Upvotes: 0
Views: 3503
Reputation: 1
I'm adding this for those who might be have the same problem/solution that I did. I am new to Laravel (5.2) and had been experimenting with different facets of authorization and routing. At one point, my LaravelCollective HTML error bag started getting lost. After much frustration I found this -
I had created a routing group for pages requiring Authorization. I placed a forms page in that group. What I forgot to do was to remove the call to the Authorize Middleware from the controller for that page - $this->middleware('auth'); This caused the request to call for authorization twice. It did not force me to log in twice however, it did cause the session to be recreated and all previous save data was lost. I would end up with MANY session files after running the page a few times.
Upvotes: 0
Reputation: 623
laravel 5.2.*
/* app/Http/Kernel.php */
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel {
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
/* you need these two Middleware */
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
]
}
Upvotes: 0
Reputation: 1580
Why do you not use built-in register function ? If you want you also could write your own register with Validator https://laravel.com/docs/master/validation
$validator = \Validator::make($request->all(), User::$login_validation_rules);
if($validator->passes()) {
// authenticate...
// If auth failed, add message to validator
$validator->getMessageBag()->add('password', 'Email or password invalid');
}
return redirect()->back()->withErrors($validator)->withInput();
The issue is you only handle input error, if user type invalid email or password, it not show.
Upvotes: 0
Reputation: 587
I think you can modify your handleLogin method to send the errors to the view, something like this:
public function handleLogin(Request $request)
{
$validator=$this->validate($request, User::$login_validation_rules);
$data = $request->only('email', 'password');
if(\Auth::attempt($data)) {
return redirect()->intended('home');
}
return back()->withInput()->withErrors($validator->errors());
}
It should work
You can review the solution reading the Laravel Documentation: https://laravel.com/docs/5.0/validation
Regards!
Upvotes: 2