【问题标题】:Validator for current password, new password and confirm new password laravel当前密码、新密码和确认新密码的验证器 laravel
【发布时间】: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


    【解决方案1】:

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

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

    如果你使用 laravel >=5.5,你可以使用创建规则类

    php artisan make:rule IsCurrentPassword
    

    如果您使用的是 laravel

    您的 pass 方法可能如下所示:

    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()), ..],
        ...
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-26
      • 2023-03-31
      • 2013-09-18
      • 1970-01-01
      • 2015-04-03
      • 2023-02-23
      • 1970-01-01
      • 2011-06-09
      相关资源
      最近更新 更多