2016-09-01 5 views
0

Yiiに存在する検証に問題があります。私のコードを実行すると、バリデーションが実行されていません。私はその問題が何であるか分からない。ここでExist Validationが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, 
     )); 
    } 

はありがとうございました...私を助けてください。あなたが必要とするいくつかの列をしたい場合は

答えて

0

は、あなたたちは、あなたが達成しようとしているのかを知る必要があり、これを答えるために

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'), 
     ); 
    } 
1

のようなコードを書く必要があります。あなたの確認のためのあなたの存在バリデータのパラメータに基づいて、私はあなたがチェックするか一意にしようとしていると推測しています。

ユーザー名は、すべてのレコードを一意であるかどうかを確認する必要がある場合は、それのためのバリデータがあります:http://www.yiiframework.com/doc/api/CUniqueValidatorが。

そうしないと、あなたは何をしたいかを指定し、この基準をチェックする必要があります。 http://www.yiiframework.com/wiki/56/reference-model-rules-validation/

+0

はい、あなたは正しいです。新しいユーザーを追加するときに、ユーザー名をデータベースにチェックしたい私は独自のバリデータを使用しようとします。ありがとうございました。 –

+0

@RizaldiMaulidiaこの回答または他の人があなたの問題を助けたり解決した場合は、それを受け入れることができます。 –

+0

それは仕事です!ありがとうございました。私はユニークな検証を使用してみました。 –

関連する問題