2017-04-07 75 views
0

我有一個模型,具體如下驗證規則衝突的警予模型場景

public function rules() 
{ 
    // NOTE: you should only define rules for those attributes that 
    // will receive user inputs. 
    //post of step 1 
    if (isset($_POST['yt0'])) 
    { 
     return array(
       array('EmailAddress,', 'required','message' => Yii::t('message', 'USERNAME_REQUIRED')), 
       array('Password,', 'required','message' => Yii::t('message', 'PASSWORD_REQUIRED')), 
       array('confirmPassword,', 'required','message' => Yii::t('message', 'CONFIRM_PASSWORD_REQUIRED')), 
       array('Password','length', 'max' => 100, 'min' => 6, 'tooShort' => Yii::t('message', 'PASSWORD_LENGTH')), 
       array('confirmPassword', 'compare', 'compareAttribute'=>'Password','message' => Yii::t('message', 'PASSWORD_COMPARE')), 
       array('EmailId,', 'required','message' => Yii::t('message', 'EMAILID_REQUIRED')), 
       array('EmailId','email','message'=>Yii::t('message', 'EMAILID_VALID')), 
       array('chapterCode,', 'required','message' => Yii::t('message', 'CHAPTERCODE_REQUIRED')), 
       array('verifyCode,', 'required','message' =>Yii::t('message', 'VERIFYCODE_REQUIRED')), 
       array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'caseSensitive'=>true,'message' =>Yii::t('message', 'VERIFYCODE_INCORRECT')), 
       array('EmailAddress', 'unique','className'=>'User','attributeName'=>'EmailAddress','message'=>Yii::t('message', 'EMAILID_UNIQUE')), 
       array('PersonId', 'unique','className'=>'User','attributeName'=>'PersonId','message'=>"Person already exists."), 
       array('FailedLoginCount', 'safe'), 
     ); 
    } elseif (isset($_POST['yt1'])) 
    { 
     return array(
       array('EmailAddress,', 'required','message' => Yii::t('message', 'USERNAME_REQUIRED')), 
       array('Password,', 'required','message' => Yii::t('message', 'PASSWORD_REQUIRED')), 
       array('FailedLoginCount', 'safe'), 

     ); 
    }elseif (isset($_POST['savecontact'])||$this->memBelongsto==0) 
    { 
     return array(
       array('EmailAddress,', 'required','message' => Yii::t('message', 'USERNAME_REQUIRED')), 
       array('Password,', 'required','message' => Yii::t('message', 'PASSWORD_REQUIRED')), 
       array('confirmPassword', 'required','message' => Yii::t('message', 'CONFIRM_PASSWORD_REQUIRED')), 
       array('Password','length', 'max' => 100, 'min' => 6,'tooShort' => Yii::t('message', 'PASSWORD_LENGTH')), 
       array('confirmPassword', 'compare', 'compareAttribute'=>'Password','message' => Yii::t('message', 'PASSWORD_COMPARE')), 
       array('EmailId,', 'required','message' => Yii::t('message', 'EMAILID_REQUIRED'),'except'=>'datavalid'), 
       array('EmailId','email','message'=>Yii::t('message', 'EMAILID_VALID'),'except'=>'datavalid'), 
       array('FailedLoginCount', 'safe'), 

     ); 
    }else{ 
     return array(
       array('EmailAddress,EmailId, Password', 'required'), 
       array('PersonId, ActiveFlag, FailedLoginCount', 'numerical', 'integerOnly'=>true), 
       array('EmailAddress, Password', 'length', 'max'=>100), 
       array('confirmPassword,', 'required','message' => Yii::t('message', 'PASSWORD_REQUIRED')), 
       array('Password','length', 'max' => 100, 'min' => 6, 'tooShort' => Yii::t('message', 'PASSWORD_LENGTH')), 
       array('confirmPassword', 'compare', 'compareAttribute'=>'Password','message' => Yii::t('message', 'PASSWORD_COMPARE')), 
       array('chapterCode,', 'required','message' => Yii::t('message', 'CHAPTERCODE_REQUIRED')), 
       array('verifyCode,', 'required','message' =>Yii::t('message', 'VERIFYCODE_REQUIRED')), 
       array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'caseSensitive'=>true,'message' =>Yii::t('message', 'VERIFYCODE_INCORRECT')), 
       array('FailedLoginCount', 'safe'), 
     ); 
    } 
} 

驗證規則和這裏yt0yt0規則將被應用提交註冊頁面上,因此對註冊按鈕的名字,我有另一種在該頁面上更改密碼的表單我只需要三個字段,更改密碼的提交按鈕是savecontact,但會應用savecontact規則,但註冊按鈕規則衝突,因此註冊驗證無效。是否正確的方式正確的規則如上任何建議請......

回答

1

雖然這可能工作,你是在做錯誤的方法。 您應該使用scenario來執行這些類型的任務。

首先,您可以爲不同的形式申報的情況:

class User extends ActiveRecord 
{ 
    const SCENARIO_LOGIN = 'login'; 
    const SCENARIO_SIGNUP = 'signup'; 
    const SCENARIO_UPDATE = 'update'; 
} 

當你與定義的場景進行,你可以告訴你的模型是什麼屬性,使用大量的任務和特定的應用驗證場景。

class User extends ActiveRecord 
{ 
    const SCENARIO_LOGIN = 'login'; 
    const SCENARIO_REGISTER = 'register'; 
    const SCENARIO_UPDATE = 'update'; 

    public function scenarios() 
    { 
     return [ 
      self::SCENARIO_LOGIN => ['username', 'password'], 
      self::SCENARIO_REGISTER => ['email', 'username', 'password'], 
      self::SCENARIO_UPDATE => ['email', 'username', 'password'], 
     ]; 
    } 
} 

現在你已經完成了,你可以選擇添加驗證規則或使用由gii生成的默認規則。

class User extends ActiveRecord 
{ 
    const SCENARIO_LOGIN = 'login'; 
    const SCENARIO_REGISTER = 'register'; 
    const SCENARIO_UPDATE = 'update'; 

    public function scenarios() 
    { 
     return [ 
      self::SCENARIO_LOGIN => ['username', 'password'], 
      self::SCENARIO_REGISTER => ['email', 'username', 'password'], 
      self::SCENARIO_UPDATE => ['email', 'username', 'password'], 
     ]; 
    } 

    public function rules() 
    { 
     return ArrayHelper::merge([ 
      ['email' => 'email', 'message' => Yii::t('message', 'wrong email format')], 
      [['email', 'password', 'username'] => 'email'], 
      ['username', 'exist', 'on' => self::SCENARIO_LOGIN], // this will apply only on login 
      // other rules 
     ], parent::rules()); 
    } 
} 

現在,你與你的模型場景和規則進行,時間要正確地使用它們。

頭到控制器,並使用情景實例化你的模型:

public function actionLogin() 
{ 
    $model = new User(['scenario' => User::SCENARIO_LOGIN]); 

    if($model->load(Yii::$app->request->post())) { 
     // $model will have username and password even if email is in the form since it is using 
     // User::SCENARIO_LOGIN 
     $model->validate(); 
     // will validate only username and password even if email is in the form 
     // will also use the username exist validator 
     // do your stuff 
    } 
} 

public function actionRegister() 
{ 
    $model = new User(['scenario' => User::SCENARIO_REGISTER]); 

    if($model->load(Yii::$app->request->post())) { 
     // $model will have username, password and email 
     $model->validate(); 
     // will validate email, username and password but will not use the 
     // username exist validation rule since it has the `'on' => self::SCENARIO_LOGIN` 
     // do your stuff 
    } 
} 

轉到http://www.yiiframework.com/doc-2.0/guide-structure-models.html關於如何正確使用模型和驗證

+0

感謝兄弟的答覆,我會嘗試這一點,更多的細節我會問是否有任何疑問 – rch