我在请求(Laravel请求验证)文件中使用以下代码
public function rules()
{
return [
'email'=>'required|email',
'password'=>'required'
];
}
public function messages()
{
return [
'email.required' => 'Please enter email ',
'email.email' => 'Please enter valid email ',
'password.required' => 'Enter your password.',
];
}
如果登录凭据与用户表“错误的电子邮件/ ID或密码”不匹配,我想显示验证错误,是否可以在此处编写代码,或者我应该如何在控制器中编写代码。< / p>
先谢谢。
答案 0 :(得分:5)
你可以试试这个:
public function rules($data){
$messages = [
'email.required' => 'Please enter email ',
'email.exists' => 'Email not registered',
'email.email' => 'Please enter valid email ',
'password.required' => 'Enter your password.',
];
$validator = Validator::make($data, [
'email' =>'required|email|exists:users',
'password' =>'required'
], $messages);
return $validator;
}
并在控制器中:
$validator = $this->rules($request->all());
if($validator->fails()){
return redirect()->back()->withErrors($validator)->withInput();
}
else{
if (Auth::attempt(['email' => $email, 'password' => $password])) {
//login successful
}
else{
return Redirect::back()
->withInput()
->withErrors([
'password' => 'Incorrect password!'
]);
}
}
答案 1 :(得分:0)
这应该是基本代码 -
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return redirect()->intended('dashboard');
}
了解更多相关信息。 https://laravel.com/docs/5.2/authentication#authenticating-users