参考了https://getyii.com/topic/221
但是我没搞明白:’on’ => [‘default’, ‘login_sms_code’]]什么含义? ‘on’ => [‘login_sms_code’]]?
['mobile', 'required', 'on' => ['default', 'login_sms_code']],
['mobile', 'integer', 'on' => ['login_sms_code']],
['mobile', 'match', 'pattern' => '/^1[0-9]{10}$/', 'on' => ['default', 'login_sms_code'], 'message' => '{attribute}必须为1开头的11位纯数字'],
['mobile', 'string', 'min' => 11, 'max' => 11, 'on' => ['default', 'login_sms_code']],
['mobile', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This mobile has already been taken.'],
['smsCode', 'required','on' => ['default','login_sms_code']],
['smsCode', 'integer','on' => ['default','login_sms_code']],
['smsCode', 'string', 'min'=>6,'max' => 6,'on' => ['default','login_sms_code']],
['smsCode', 'required','requiredValue'=>$this->getSmsCode(),'on' => ['default','login_sms_code'],'message'=>'手机验证码输入错误'],
];
yii\base\Model里面有个scenario的概念,中文翻译过来就是‘情景模式’的意思;以你的代码为例,
1.$model->setScenario(‘login_sms_code’);
2.$model->validate();
3.这时你的这个验证才会生效,不然这个验证是不会生效的
4.补充一点,默认的scenario是’default’
on 是场景 看文档吧