当前密码,新密码和确认新密码laravel的验证器

时间:2018-06-18 12:09:11

标签: laravel validation passwords confirmation

您好我在验证时遇到问题这是我的控制器:

<?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',
            ]; 
    }
}

正如您在控制器上看到的那样,我正在使用哈希检查将用户在表单上键入的密码与旧密码进行比较。如果旧密码错误,如何显示验证消息,如何进行验证: enter image description here

另外两个新密码与确认新密码时的密码不相同,以显示如下信息: enter image description here

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以创建自定义验证规则,以检查当前密码是否正确。

然后,在您的刀片文件中,您可以添加条件以显示包含当前密码字段的错误消息。

如果您使用的是laravel&gt; = 5.5,则可以使用

创建规则类
php artisan make:rule IsCurrentPassword

如果你正在使用laravel&lt; 5.5,您可以使用相同的逻辑创建自定义验证。

你的传递方法可能如下所示:

public function __construct($user)
{
     $this->user = $user;
 }
public function passes($attribute, $value)
{
    return Hash::check($value, $user->password);
}

您可以使用message方法返回自定义错误消息。

然后在你的验证数组中

[
    'current_password' => [ ... , new IsCurrentPassword (auth()->user()), ..],
    ...
]