【问题标题】:Laravel Current password validation check is not working in front endLaravel 当前密码验证检查在前端不起作用
【发布时间】:2020-06-15 10:56:09
【问题描述】:

我正在使用 JS Validator 在 Laravel 中验证我的表单字段

以下是我在用户控制器中用于验证的代码

class UserController extends Controller
{
    protected $changepassValidationRules = [
        'old_password' => 'required|pwdvalidation|min:6',
        'password' => 'required|min:6|confirmed',
    ];
}

我通过$changepassValidationRules查看页面进行检查。

以下是我在视图页面中的代码

<div class="col-md-4 col-sm-6 col-xs-12">
    <div class="card">
        <form method="post" action="{{ url('user/changepassword') }}" name="useredit" id="changepass"
              enctype="multipart/form-data" novalidate>
            {{ csrf_field() }}
            @if (session('message_changepass'))
            <div class="alert alert-success">
                {{ session('message_changepass') }}
            </div>
            @endif
            <div class="card-header">
                <h4 class="card-title">
                    Change Password
                </h4>
            </div>

            <div class="card-content">
                <div class="form-group {{ $errors->has('old_password') ? ' has-error' : '' }}">
                    <label class="control-label">Old Password
                        <star> *</star>
                    </label>
                    <input type="password" placeholder="Password" class="form-control" id="old_password"
                           name="old_password">
                    @if ($errors->has('old_password'))
                    <span class="help-block error-help-block">
                                    {{ $errors->first('old_password') }}
                                </span>
                    @endif
                </div>
                <div class="form-group {{ $errors->has('password') ? ' has-error' : '' }}">
                    <label class="control-label">New Password
                        <star> *</star>
                    </label>
                    <input type="password" placeholder="Password" class="form-control" id="password" name="password">
                    @if ($errors->has('password'))
                    <span class="help-block">
                                    {{ $errors->first('password') }}
                                </span>
                    @endif
                </div>

                <div class="form-group">
                    <label class="control-label">Confirm Password
                        <star> *</star>
                    </label>
                    <input id="password-confirm" type="password" class="form-control" name="password_confirmation"
                           placeholder="Confirm Password">
                </div>
                <div class="form-action">
                    <button type="submit" class="btn btn-fill btn-info">Submit</button>
                </div>
            </div>
        </form>
    </div>

所有其他验证规则都运行良好,但问题是我想验证数据库中已经存在的当前密码。

如果用户输入错误的密码应该抛出错误,这是什么问题

【问题讨论】:

  • pwdvalidation 是什么?

标签: php laravel


【解决方案1】:

您可以使用Custome Validate Rule 创建您自己的客户验证

1.我将创建 MatchCurrentPassword 规则

php artisan make:rule MatchCurrentPassword

2.在规则代码中

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Hash;

class MatchCurrentPassword implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return Hash::check($value, auth()->user()->password);
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return ':attribute is not match';
    }
}

  1. 在你的用户控制器中
use App\Rules\MatchCurrentPassword;

class UserController extends Controller
{
    public function changePassword(Request $request) {
        $request->validate([
            'old_password' => ['required', 'min:6', new MatchCurrentPassword],
            'password' => 'required|min:6|confirmed',
        ]);

        // you logic code 
    }
}

您也可以将请求移动到表单请求Form Request Validation

希望对你有帮助

【讨论】:

【解决方案2】:

您可以在当前密码模糊时触发 ajax 请求,并在控制器中编写代码以匹配用户输入的当前密码是否正确,如果不正确则返回 false,它将进入错误的回调函数,您可以从中显示错误。

$("$crntPwd").on("blur",function(){
$.ajax({
url:'your url',
method: 'get/post',
data : {current pwd's value},
success:function(response){
console.log(response);
},
error:function(error){
console.log(error or "your current password is incorrect");
}
});
});

【讨论】:

    猜你喜欢
    • 2015-06-24
    • 2015-10-15
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 2017-04-27
    相关资源
    最近更新 更多