简体   繁体   中英

Laravel Current password validation check is not working in front end

I am using JS Validator to validate my form fields in Laravel

Following is my code in usercontroller for validation

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

I am passing $changepassValidationRules to view page for checking.

Following is my code in view page

<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>

All other validations rules are working perfectly, but issue is that I want to validate current password already in DB.

if user enters a wrong password error should be thrown, what is the problem here

You can useCustome Validate Rule to create your own custome validate

1.I will create MatchCurrentPassword Rule

php artisan make:rule MatchCurrentPassword

2.In rule code

<?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. in your UserController
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 
    }
}

you can move request to form request also Form Request Validation

Hope it help

You can trigger ajax request on current password's blur and write code in controller to match the current password that the use has entered is correct or not if not then return false and it will come in error's callback function from where you can show the error .

 $("$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"); } }); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM