Php Yii2:ActiveForm字段数字,长度=>;8.

Php Yii2:ActiveForm字段数字,长度=>;8.,php,yii2,yii2-advanced-app,Php,Yii2,Yii2 Advanced App,我正在尝试使用yii2来验证我的ActiveForm字段,该字段应该是8个字符长的数字字段 以下是我在yii2/advanced/backend的默认LoginForm模型中尝试的内容,但不幸的是,是数字的验证程序根本无法启动: public function rules() { return [ // username and password are both required [['username', 'password'], 'required'

我正在尝试使用yii2来验证我的
ActiveForm
字段,该字段应该是8个字符长的数字字段

以下是我在
yii2/advanced/backend
的默认
LoginForm
模型中尝试的内容,但不幸的是,
是数字的
验证程序根本无法启动:

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

我还尝试按照相关帖子()中的建议添加一个场景,但只有在场景中没有包含
密码
字段的情况下,才有效(如错误中所示)

这是实现这一目标的好方法,还是你能想出更好的方法


注意:我之所以将
username
定义为
string
,是因为数字可能包含前导0。

请阅读有关验证的更多信息:

使用yii2basic中的联系人表单,这对我来说很好

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

尝试使用
integer
数据类型:

[['username'], 'integer'],
[['username'], 'string', 'min' => 8],
它将验证
数值
长度
。这就可以了。

公共函数规则()
{
返回[
//用户名应该是数字
['username','match','pattern'=>'/^\d{8}$/','message'=>'字段必须正好包含8位数字。],
// ...
];
}
试试这个:

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'], 'number'],
    [['subject'], 'string', 'max' => 8, 'min' => 8],
    // verifyCode needs to be entered correctly
    ['verifyCode', 'captcha'],
];
}

抱歉,但是这将如何使它成为8位数字,并且还考虑引导0的?不,不工作(没有场景),我把一个简单的字符串在用户名字段的字母,它验证罚款。同样,它适用于该场景,但前提是我不使用密码字段。我认为问题不在于验证器功能,而在于处理场景的方式,或者两个验证器无法“协同工作”(?)。。。说真的,不知道,所以我的问题…非常有效。不知道你在说什么情景。我测试了我的代码,它只对我有用。请查看我对已接受答案的评论,确保您希望在字段中接受+和-。您是对的,我的问题是这样问的,这是正确的答案。非常感谢。我在对另一个答案的评论中解释了实际问题,但为了确保保留历史记录(对于删除一个的情况),这里再次说明:我希望form类中的customValidator函数能够在客户端工作。结果表明,只有单独设置自定义验证器类时,这才有效,请参阅此
min
的更多信息。在这种情况下,将检查最小值为8的数字,而不是长度为8的字符,即
01
将通过,而且不应该…我可以发誓我尝试过这个。。。无论如何,这是工作,所以谢谢你-1231231和+1231231将通过您的检查,因为它是一个整数,长度为8个字母。“我想没关系。”米哈伊普说。老实说,我没有想到正负号,这是个好主意。。。我认为整个问题归结于这样一个事实,即自定义验证器不会自动在客户端工作,根据我另一个问题的评论,对不起,我不得不接受@MihaiP.的正确答案。纯粹从这个问题的角度来看,他是对的。他的答案以前不起作用的原因是我希望表单类中的customValidator函数在客户端工作。事实证明,只有单独设置一个自定义验证器类,这才会起作用,请参阅更多信息这与它不太完整几乎是一样的…我不明白为什么您认为规则['username'、'match'、'pattern'=>'/^\d{8}$/','message'=>'字段必须正好包含8位数字]]是不完整的。方法isNumeric()只执行我的建议,但它有更多的代码行,这不是最佳解决方案。首先,你甚至没有包含一条错误信息,其次,你的解决方案,再一次,与之前的答案几乎完全相同,所以它不是真正有用的。。。但看看社区是如何决定的(如果你能得到投票…@Dave2e,答案同上)