2014-03-31 138 views
1

我有一个输入验证更改用户密码,当我试图向我得到总是新密码和确认密码甚至不匹配错误的形式,这是我的帖子行动:laravel中的输入验证?

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

这是我的规则:

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

所以请,如果有人有一个想法,我将非常感激

回答

1

这是因为Laravel预期(为您的具体情况)confirmed场被命名为new_password_confirmation

来自doc“验证字段必须具有foo_confirmation的匹配字段。例如,如果在验证字段是密码,匹配password_confirmation字段必须存在于输入“

这样的规则应该像(也改变形式输入名称):

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

或者你可以使用same验证规则(如果不想更新表单输入):

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