2015-10-11 111 views
6

我試圖讓yii2來驗證我的ActiveForm字段應該是數字和8個字符長。Yii2:ActiveForm字段數字,長度=> 8

下面是我試過在yii2/advanced/backend默認LoginForm模式,但不幸的是,isNumeric驗證只需在不踢:

public function rules() 
{ 
    return [ 
     // username and password are both required 
     [['username', 'password'], 'required'], 
     // username should be numeric 
     ['username', 'isNumeric'], 
     // username should be numeric 
     ['username', 'string', 'length'=>8], 
     // password is validated by validatePassword() 
     ['password', 'validatePassword'], 
    ]; 
} 

/** 
* Validates if the username is numeric. 
* This method serves as the inline validation for username. 
* 
* @param string $attribute the attribute currently being validated 
* @param array $params the additional name-value pairs given in the rule 
*/ 
public function isNumeric($attribute, $params) 
{ 
    if (!is_numeric($this->username)) 
     $this->addError($attribute, Yii::t('app', '{attribute} must be numeric', ['{attribute}'=>$attribute])); 
} 

/** 
* Validates the password. 
* This method serves as the inline validation for password. 
* 
* @param string $attribute the attribute currently being validated 
* @param array $params the additional name-value pairs given in the rule 
*/ 
public function validatePassword($attribute, $params) 
{ 
    if (!$this->hasErrors()) { 
     $user = $this->getUser(); 
     if (!$user || !$user->validatePassword($this->password)) { 
      $this->addError($attribute, 'Incorrect username or password.'); 
     } 
    } 
} 

我也嘗試添加的場景作爲一個建議(https://stackoverflow.com/a/27817221/2037924),但只有在該場景中沒有包含password字段的情況下才起作用(如顯示錯誤)。

這是一個很好的方法來實現這一點,或者你能想出一個更好的方法嗎?

注:我定義usernamestring的原因是因爲數字可能包含前導0。

回答

6

瞭解更多關於這裏的驗證:http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#number

這種利用yii2-基本的聯繫方式工作正常,我

/** 
* @return array the validation rules. 
*/ 
public function rules() 
{ 
    return [ 
     // name, email, subject and body are required 
     [['name', 'email', 'subject', 'body'], 'required'], 
     // email has to be a valid email address 
     ['email', 'email'], 
     ['subject', 'is8NumbersOnly'], 
     // verifyCode needs to be entered correctly 
     ['verifyCode', 'captcha'], 
    ]; 
} 

public function is8NumbersOnly($attribute) 
{ 
    if (!preg_match('/^[0-9]{8}$/', $this->$attribute)) { 
     $this->addError($attribute, 'must contain exactly 8 digits.'); 
    } 
} 
+0

對不起,但這將如何使它8位數字,也考慮領先0? – webeno

+0

nope,不工作(沒有這種情況),我在用戶名字段中輸入了一個帶有字母的簡單字符串,它驗證了正常。再次,它適用於這種情況,但只有當我離開密碼字段時纔有效。我相信問題不在於驗證器功能,而在於處理場景的方式,或者2驗證器不會「一起工作」(?)...認真,不知道,因此我的問題... – webeno

+0

完美的作品。不知道你在說什麼場景。我測試了我的代碼,它只適用於我。 –

7

integer數據類型嘗試:

[['username'], 'integer'], 
[['username'], 'string', 'min' => 8], 

它將同時驗證numericlength。這將做到這一點。

+0

'min'在這種情況下會檢查一個數值,最小值爲8,而不是字符長度爲8,即。 '01'會通過,而且不應該... – webeno

+0

我可以發誓我試過這個......反正這是工作,所以謝謝! – webeno

+1

-1231231和+1231231將通過您的支票,因爲它是一個整數,它是8個字母長。我想這沒關係。 –

1
public function rules() 
{ 
    return [ 
     // username should be numeric 
     ['username', 'match', 'pattern' => '/^\d{8}$/', 'message' => 'Field must contain exactly 8 digits.], 

     // ... 
    ]; 
} 
+2

這與[Mihai P.已經接受的答案](http://stackoverflow.com/a/33071398/2037924)幾乎相同,它只是不完整... – webeno

+0

是的,你是對的。然而越簡單越好。 –

+0

如果它是相同的,但不完整,這並沒有使它更好,我不認爲... – webeno