Be late, but another solution is to add a custom message to the bag with the error message:
controller
$rules = array(
'email' => 'required|exists:users,email|email|max:32',
'password' => 'required|max:20'
);
$validator = Validator::make($request->all(),$rules);
$email = $request->email;
$password = $request->password;
$validateUser = new user();
$users = $validateUser::where('email', $email)->get();
if($users->isEmpty()){
$validator->getMessageBag()->add('email', 'Invalid Email Address');
return redirect('home')->withErrors($validator);
}
foreach ($users as $user) {
$data = $user->showAdminData();
if($user->role_id!=1){
$validator->getMessageBag()->add('email', 'Unauthorised access');
}
if(Crypt::decrypt($user->password)!==$password){
$validator->getMessageBag()->add('password', 'Invalid Password');
}
}
return redirect('home')->withErrors($validator);
View
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
@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">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
source
share