1

I have a Input validation to change user password, when i tried to submit the form i got always an error that the new password and confirm password are not matched even, this is my post action :

public function doChangePassword()
{
    if(Auth::check())
    {
    $validator = Validator::make(Input::all(), User::$updatePasswordRules);

    // if the validator fails, redirect back to the form
    if ($validator->fails()) {
        return Redirect::to('change-password')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
    } else {
        // store
        $user = User::find(Auth::user()->id);
        if(Auth::user()->password==Input::get('new_password')){
        $user->password = Hash::make(Input::get('new_password'));
        $user->save();
        }
        else{
            return Redirect::to('change-password')->with('message', 'The password is not correct');
        }

        // redirect
        Session::flash('message', 'Successfully updated password!');
        return Redirect::to('login');
    }
    }
    else{
        return Redirect::to('login');
    }
}

this is my rules :

 public static $updatePasswordRules = array(
    'password'=>'required|alpha_num|between:6,12',
    'new_password'=>'required|alpha_num|between:6,12|confirmed',
    'password_confirmation'=>'required|alpha_num|between:6,12'
);

so please if someone has an idea i will be very appreciative

1 Answer 1

1

It's because Laravel expects (for your specific case) confirmed field to be named new_password_confirmation

From doc "The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input."

Thus rules should look like (also change input name in form):

 public static $updatePasswordRules = array(
    'password'=>'required|alpha_num|between:6,12',
    'new_password'=>'required|alpha_num|between:6,12|confirmed',
    'new_password_confirmation'=>'required|alpha_num|between:6,12'
);

Or you can do it with same validation rule (if don't want to update form inputs):

 public static $updatePasswordRules = array(
    'password'=>'required|alpha_num|between:6,12',
    'new_password'=>'required|alpha_num|between:6,12|same:password_confirmation',
    'password_confirmation'=>'required|alpha_num|between:6,12'
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.