2016-04-03 43 views
0

如何在不點擊表單按鈕的情況下驗證我的表單?如何從yii2生活驗證?

型號:

public function rules() { 
    return [ 
     ['CodeKargah', 'CodeKargah_check'], 
     // ... 
     // ... 
     // ... 
    ]; 
} 

public function CodeKargah_check($attribute) { 
    $zero = substr($this - > $attribute, 0, 1); 
    $len = strlen((string)($this - > $attribute)); 
    if ($zero == '0' && $len == 10) { 
     return null; 
    } 
    else { 
     $this - > addError($attribute, Yii::t('app', 'First number must be zero')); 
    } 
} 

提交表格後,它的工作原理

+0

請張貼之前修復的代碼縮進 – Druzion

回答

0

我解決了:

public function rules() { 
    return [ 
      ['CodeKargah', 'integer', 'min' => 100000000, 'tooSmall' => Yii::t('app', 'The value entered must be 10 digits.')], 
      ['CodeKargah', 'match', 'pattern' => '/^0/'], 
     // ... 
     // ... 
     // ... 
    ]; 
}