簡體   English   中英

使用yii框架注冊時遇到麻煩

[英]trouble with registration using yii framework

我正在嘗試使用yii ActiveRecord來實現用戶注冊。 問題是,每次加載視圖時,我都會收到以下異常:

User has an invalid validation rule. 
The rule must specify attributes to be validated and the validator name. 

這是用戶模型的相關部分:

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('username, password,verifyCode,email','message'=>'Tidak boleh kosong'),
            //array('password','compare'),
            array('verifyCode', 'captcha', 'required', 'allowEmpty'=>!extension_loaded('gd')),
            array('level_id', 'numerical', 'integerOnly'=>true),
            array('username', 'length', 'max'=>20),
            array('password, email', 'length', 'max'=>50),
            //array('password_repeat','required'),
            array('avatar', 'file', 'types'=>'gif,png,jpg'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, username, password, email, joinDate, level_id, avatar', 'safe', 'on'=>'search'),
    );
}

控制器:

public function actionRegister() {
    $registration = new RegistrationForm("register");
    // collect user input data
    if (isset($_POST['RegistrationForm'])) {
        $registration->attributes = $_POST['RegistrationForm'];
        $registration->attributes = $_POST['RegistrationForm'];
        // validate user input and redirect to the previous page if valid
        if ($registration->validate()) {
            // create an account model
            $account = new User;
            $account->username = $registration->username;
            $account->password = $registration->password;
            $account->email = $registration->email;
            $account->joinDate = new CDbExpression('NOW()');
            //$account->level_id = 1;
            if ($account->save()) {
                $member = new Member;
                $member->attributes = $registration->attributes;
                $member->user_id = $account->id;
                if ($member->save()) { 
                    $this->redirect(array('index'));
                } else {
                    $registration->addErrors($member->getErrors());
                }
            } else {
                $registration->addErrors($account->getErrors());
            }
        }
    }
    $this->render('register', array('model' => $registration));
}

這是因為您在第一條規則中錯過了驗證者。 我認為,您錯過了必需的驗證器。

因此,您需要更改以下內容:

array('username, password,verifyCode,email','message'=>'Tidak boleh kosong'),

對於這樣的事情:

array('username, password,verifyCode,email','required','message'=>'Tidak boleh kosong'),

另一個錯誤是,您使用驗證碼驗證器 驗證碼只能在具有CFormModelCCaptcha小部件CCaptcha操作的視圖中使用。

如果要上傳文件(不需要),則需要添加allowEmpty

array('avatar', 'file', 'allowEmpty' => true, 'types'=>'gif,png,jpg'),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM