user3438415
user3438415

Reputation: 165

Input validation in laravel?

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

Upvotes: 1

Views: 341

Answers (1)

arma
arma

Reputation: 4124

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'
);

Upvotes: 1

Related Questions