2015-10-11 112 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