簡體   English   中英

現有驗證在Yii中不起作用

[英]Exist Validation doesn't work in Yii

我在Yii中存在驗證存在問題。 當我運行代碼時,不會觸發存在驗證。 我不知道是什么問題。

這是用戶模型中的規則:

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id_cabang, username, password, nama, level', 'required', 'on'=>'create'),
            array('username, nama, level', 'required', 'on'=>'update'),
            array('id_cabang, nohp', 'numerical', 'integerOnly'=>true),
            array('username', 'length', 'max'=>20),
            array('password', 'length', 'max'=>20),
            array('nama', 'length', 'max'=>100),
            array('jabatan', 'length', 'max'=>100),
            array('nohp', 'length', 'max'=>14),
            array('level', 'length', 'max'=>10),
            array('username', 'exist', 
                'attributeName'=>'username', 
                'className'=>'User', 
                'caseSensitive'=>true, 
                'on'=>'create'
            ),
            array('username', 'exist', 
                'attributeName'=>'username', 
                'className'=>'User', 
                'caseSensitive'=>true, 
                'criteria'=>array('condition'=>'username<>:u', 'params'=>array(':u'=>$this->current_username)),
                'on'=>'update'
            ),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id_cabang, username, nama, nohp, jabatan, level', 'safe', 'on'=>'search'),
        );
    }

這是我在用戶控制器中的actionCreate:

public function actionCreate()
    {
        $model=new User;
        $model->scenario = 'create';
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['User']))
        {
            $model->attributes=$_POST['User'];
            $model->id_cabang = 1;
            if($model->save())
                $this->redirect(array('index'));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

請幫我..

謝謝。

要回答這個問題,我們需要知道您要完成什么。 根據您現有的驗證器參數進行更新,我猜測您嘗試檢查或唯一性。

如果您需要檢查用戶名在所有記錄中是否唯一,則有一個驗證器: http : //www.yiiframework.com/doc/api/CUniqueValidator

否則,您需要指定所需的內容,並檢查此參考: http : //www.yiiframework.com/wiki/56/reference-model-rules-validation/

如果您需要一些列,則應編寫如下代碼

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array(array('id_cabang', 'username', 'password', 'nama','level'), 'required','on'=>'create'),
        );
    }

暫無
暫無

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

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