表格 post 如果密码输入是 Laravel 5.2 中的当前密码
Form post if password input is current password in Laravel 5.2
我正在尝试检查输入的密码是否是当前登录用户的密码,如果不是则给出错误消息。
我有个人资料编辑表格,其中包含姓名、电子邮件等
表单最后一次输入是密码输入。
如何检查输入的密码是否为当前密码?
我尝试在 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',
];
}
但它拒绝了密码正确的表单。
然后我尝试将这部分添加到控制器中:
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']);
还要确保在登录页面中显示错误。
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
您可以在此页面上检查显示验证错误https://laravel.com/docs/5.2/validation#quick-displaying-the-validation-errors
我正在尝试检查输入的密码是否是当前登录用户的密码,如果不是则给出错误消息。
我有个人资料编辑表格,其中包含姓名、电子邮件等
表单最后一次输入是密码输入。
如何检查输入的密码是否为当前密码?
我尝试在 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',
];
}
但它拒绝了密码正确的表单。 然后我尝试将这部分添加到控制器中:
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']);
还要确保在登录页面中显示错误。
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
您可以在此页面上检查显示验证错误https://laravel.com/docs/5.2/validation#quick-displaying-the-validation-errors