Yii2规则未被应用

时间:2015-11-11 23:08:46

标签: php yii2

这是针对不在我的数据库中的变量。但是用来更新我的密码。 我在User类的开头声明它为public $newPassword;。 在我的课堂上,我有以下规则:

public function rules()
  {
    return [
        ...
        [['email'], 'string', 'max' => 150],
        [['email'], 'email'],
        [['password', 'newPassword'], 'string', 'length' => [6,20]],
        [['password', 'newPassword'], 'match', 'pattern' => '/^(?=[^\d_].*?\d)\w(\w|[!@#$%]){5,20}/', 'message' => 'Must start with a letter, contain at least 1 digit, available special chars are: !@#$%'],

    ];
}

场景

public function scenarios() 
{
    return [
        self::SCENARIO_PASSWORD => ['newPassword', 'password'],
        ...
    ];
}

方案运行正常,正则表达式也是如此。但我可以将我的新密码留空并通过验证,我真的不知道为什么?

1 个答案:

答案 0 :(得分:3)

为当前密码和新密码添加单独的required规则,以便用户不能将其留空:

[['password', 'newPassword'], 'required'],

官方文档:

相关问题