Yii2: ActiveForm field numeric, length => 8

benomatis picture benomatis · Oct 12, 2015 · Viewed 21.8k times · Source

I'm trying to make yii2 to validate my ActiveForm field that should be numeric and of 8 characters long.

Following is what I tried in the default LoginForm model of yii2/advanced/backend, but unfortunately the isNumeric validator simply doesn't kick in:

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 adding a scenario as suggested in a related post (https://stackoverflow.com/a/27817221/2037924) but that only worked (as in displayed the error) if I did not include the password field in the scenario.

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

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

Answer

Insane Skull picture Insane Skull · Oct 12, 2015

Try with integer data type:

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

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