我正在尝试检查输入的密码是否是当前登录用户的密码,如果不是,则给出错误消息。
我有包含姓名、电子邮件等的个人资料编辑表单。
表单中最后输入的是密码输入。
如何检查输入的密码是否是当前密码?
在 EditProfileRequest 中我尝试过(自定义请求文件,由 artisan 制作):
public function rules() {
return [
'first_name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users,email,'.Auth::user()->id,
'country_code' => 'digits_between:1,4|numeric',
'phone' => 'digits_between:5,12|numeric',
'password' => 'required|confirmed',
];
}
但它拒绝了密码正确的表单。 然后我尝试将这部分添加到 Controller :
if (!Hash::check(Input::get('password'), Auth::user()->password)) {
return Redirect::back()->withInputs();
}
这有效,但我没有给用户带来任何错误,而且我不知道如何添加自定义错误消息,因为我使用的是内置验证器。
最佳答案
您可以传递自定义错误消息,如下所示。
$validator = Validator::make(Input::all(), $rules);
//Here you are checking your inputs with the validation rules that you are created.
if ($validator->fails())
{
//If the validation fails, check for the password validation too and return the error.
if (!Hash::check(Input::get('password'), Auth::user()->password)) {
return Redirect::back()
->withInputs()
->withErrors(['login' => 'Your custom Error Msg'])
->withErrors($validator);
}
else{
return Redirect::back()
->withInputs()
->withErrors(['login' => 'Your custom Error Msg']);
}
}
作为您问题的简短回答,以获取自定义错误消息:
return Redirect::back()->withInputs()->withErrors(['login'=>'custom msg']);
关于php - 如果密码输入是 Laravel 5.2 中的当前密码,则表单发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35325878/