我已经使用了laravel中的验证器,但是如果用户不是经过身份验证的用户,我想添加自定义规则。这就是我所做的:
public function postSignIn(){
$validator = Validator::make(Input::all(),
array(
'email' => 'required|email',
'password' => 'required'
)
);
if($validator->fails()){
//return error message
return Redirect::to('account/signin')
->withErrors($validator)
->withInput(Input::except('password'));
}
else{
$remember = (Input::has('remember')) ? true:false;
$auth = Auth::attempt(array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'active' => 1
),$remember
);
if($auth){
return Redirect::intended('/')
->with('global', 'Welcome');
}
else{
return Redirect::to('account/signin')
->withErrors($validator, 'login');
}
}
这是我显示错误的视图
@if ($errors -> has())
<div class="alert-danger alert-dismissible fade in" role="alert" style="padding: 0.5em;">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
@foreach($errors->all() as $error)
{{ $error }}<br />
@endforeach
</div>
@endif
表单的验证工作正常,但我有问题显示未经身份验证的用户的错误。所以,任何帮助都会非常感激。
答案 0 :(得分:1)
迟到但在错误消息包中添加自定义消息的另一个解决方案是:
<强>控制器强>
$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);
查看强>
<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>
答案 1 :(得分:0)
试试这样:
<?php $message = Session::get('message'); $errors = Session::get('errors'); $status = Session::get('status'); ?>
@if($message)
<div class="infobox {{$message['type']}}-bg animated flipInY" style="margin-top:30px;">
//your functionalities
</div>
@endif
答案 2 :(得分:0)
这可能有助于实现您的目标。当用户无法进行身份验证时,我在登录表单中使用了以下代码进行自定义错误。
//Code in controller
if(Auth::attempt(Input::only('email','password')))
{
return Redirect::to('/');
}
//put your message in a session
Session::flash('msg', "Invalid email or password. Please Try again! ");
return Redirect::back();
//Code in view
<div class="error">
@if (Session::has('msg'))
{{ Session::get('msg') }}
@endif
</div>