【问题标题】:Input validation in laravel?laravel中的输入验证?
【发布时间】:2014-03-31 19:12:37
【问题描述】:

我有一个输入验证来更改用户密码,当我尝试提交表单时,我总是收到新密码和确认密码不匹配的错误,这是我的发布操作:

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

所以如果有人有想法我会非常感激

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    这是因为 Laravel 期望(针对您的具体情况)confirmed 字段被命名为 new_password_confirmation

    来自文档“验证中的字段必须具有匹配的 foo_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'
    );
    

    【讨论】:

      猜你喜欢
      • 2017-11-13
      • 2018-01-10
      • 2014-09-20
      • 1970-01-01
      • 2016-08-12
      • 2021-03-06
      • 2016-01-29
      • 2021-12-07
      相关资源
      最近更新 更多