我使用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'),*/
);
}
我在提交数据时遇到错误,可能是由于确认密码提示错误密码不匹配,即使我在两个字段中输入相同的值。
答案 0 :(得分:2)
使用
$model->save(false); // save with no validation
因为您已经在
之前直接验证了模型