Yii2: ActiveForm field number, length => 8

I am trying to do yii2 to check my ActiveForm field, which should be numeric and 8 characters long.

The following is an example that I used in the default yii2/advanced/backend model LoginForm yii2/advanced/backend , but unfortunately, the isNumeric checker just doesn't kick:

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

I also tried to add the script suggested in the corresponding post ( https://stackoverflow.com/a/312469/... ), but this only worked (as shown in the figure) if I did not include the password field in the script.

Is this a good way to achieve this at all, or can you think of a better way to do it?

Note. the reason I define username as string is because numbers can contain leading 0s.

+6
source share
3 answers

More about checks here: http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#number

This works great for me using the contact form from yii2-basic

 /** * @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.'); } } 
+6
source

Try with integer type:

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

It will check both numeric and length . This will do the trick.

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

All Articles