Yii2:ActiveForm 字段数字,长度 =>8 [英] Yii2: ActiveForm field numeric, length => 8

查看:57
本文介绍了Yii2:ActiveForm 字段数字,长度 =>8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 yii2 验证我的 ActiveForm 字段,该字段应该是数字且长度为 8 个字符.

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

以下是我在 yii2/advanced/backend 的默认 LoginForm 模型中尝试的,但不幸的是 isNumeric 验证器根本没有开始:

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

我还尝试按照相关帖子中的建议添加场景 (https://stackoverflow.com/a/27817221/2037924) 但只有当我没有在场景中包含 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?

注意:我将 username 定义为 string 的原因是因为数字可能包含前导 0.

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

推荐答案

在此处阅读有关验证的更多信息:http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#number

Read more about the validations here: http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#number

使用来自 yii2-basic 的联系表格这对我来说很好

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

这篇关于Yii2:ActiveForm 字段数字,长度 =>8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆