在yii中保存到数据库之前散列密码


Hashing password before saving to databse in yii

我使用yii框架构建一个小型的工作站点应用程序。并且遇到了下面提到的错误

控制器类如下:

  public function actionRegister()
{
        $model = new RegisterForm();
    if (isset($_POST['RegisterForm'])) {
        //  print_r($_POST); exit();
        $model->attributes = $_POST['RegisterForm'];
        if ($model->validate()) {
            $model->password = sha1($model['password']);
            $model->role ='user';
            $model->status='1';
            $model->created =date("Y-m-d H:i:s");
            $model->modified =date("Y-m-d H:i:s");
            if ($model->save()) {
                Yii::app()->user->setFlash('success', "Data saved!");
            } else {
                Yii::app()->user->setFlash('error', "Error,Cannot Save Data!");
            }
        } else {
            Yii::app()->user->setFlash('error', "Validation failed!");
        }
    }
    $this->render('register', array('model' => $model));
}

和验证规则为:

public function rules()
    {
               return array(
            // All Fields Required  //
            array('username, password,contact,email,name','required'),
                        array('email','email'),
                        array('contact', 'match', 'pattern'=>'/^[0-9]{1,15}$/'),
                        array('email','validateEmail'),
                        array('repassword', 'compare', 'compareAttribute'=>'password' ,'message'=>"Passwords don't match")
                    // password needs to be authenticated
            /*array('password', 'authenticate'),*/
        );
    }

我在提交数据时遇到了一个错误,也许是由于确认密码提示错误密码不匹配,即使我在两个字段中键入相同的值

使用

$model->save(false); // save with no validation

因为您已经在

之前直接验证了模型