【发布时间】:2018-11-27 06:50:35
【问题描述】:
您好,我在验证这是我的控制器时遇到问题:
<?php
namespace App\Http\Controllers\website\settings;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\PasswordAccountRequest;
use Auth;
use Hash;
use App\User;
use Validator;
class AccountSettingsController extends Controller
{
public function changepassword(){
return view('website.settings.account.password');
}
public function newpassword(PasswordAccountRequest $request){
$auth_user_password = Auth::user()->password;
$new_password = bcrypt($request['new_password']);
if (Hash::check($request->input('current_password'), $auth_user_password)) {
$user_profile = User::where('id', Auth::user()->id)->update(['password' => $new_password]);
}
return redirect()->back()->with(['status' => 'Password changed successfully.']);
}
}
我在刀片上有我的表单:
<form role="form" class="form" action="{{ url('/settings/password-change') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="row">
<div class="col-md-12">
<label class="notifications-label"> @lang('general.change_account_pass') </label><br>
<label class="will-recieve-notifications">@lang('general.requested_a_pass_change') </label>
</div>
</div>
<br>
<div class="row padding-30 padding-top-bottomm-0">
<div class="col-md-6">
<div class="form-group floating-label {{ $errors->has('current_password') ? ' has-error' : '' }}">
<input class="form-control" name="current_password" id="regular2" type="text" value="" >
<label for="regular2">@lang('general.current_password')</label>
@if ($errors->has('current_password'))
<span class="help-block">
<strong>{{ $errors->first('current_password') }}</strong>
</span>
@endif
</div>
</div>
</div>
<div class="row padding-30 padding-top-bottomm-0">
<div class="col-md-6">
<div class="form-group floating-label {{ $errors->has('new_password') ? ' has-error' : '' }}">
<input class="form-control" name="new_password" id="regular2" type="text" value="" >
<label for="regular2">@lang('general.new_password')</label>
@if ($errors->has('new_password'))
<span class="help-block">
<strong>{{ $errors->first('new_password') }}</strong>
</span>
@endif
</div>
</div>
</div>
<div class="row padding-30 padding-top-bottomm-0">
<div class="col-md-6">
<div class="form-group floating-label {{ $errors->has('new_password') ? ' has-error' : '' }}">
<input class="form-control" name="confirm_new_password" id="regular2" type="text" value="" >
<label for="regular2">@lang('general.confirm_new_password')</label>
@if ($errors->has('new_password'))
<span class="help-block">
<strong>{{ $errors->first('new_password') }}</strong>
</span>
@endif
</div>
</div>
</div>
<div class="row settings-footer">
<div class="col-md-12 padding-0">
<button type="submit" class="btn save-lang">@lang('buttons.save_changes')</button>
</div>
</div>
</form>
这是我放置验证的模型:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PasswordAccountRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'current_password' => 'required',
'new_password' => 'required ',
'confirm_password' => 'required |same:new_password',
];
}
}
正如您在控制器上看到的,我正在使用哈希检查将用户在表单上键入的密码与旧密码进行比较。如果旧密码错误以显示如下验证消息,如何进行验证:
有人可以帮帮我吗?
【问题讨论】:
标签: laravel validation passwords confirmation