zm
2021-04-02 d6aa91642bb270e8309831d85aa1e7e09629e4da
commit | author | age
90c639 1 <?php
Z 2 /**
3  * Created by IntelliJ IDEA.
4  * User: luwei
5  * Date: 2019/3/29
6  * Time: 15:07
7  */
8
9 namespace app\validators;
10
11
12 use app\models\CoreValidateCode;
13 use yii\validators\Validator;
14
15 class ValidateCodeValidator extends Validator
16 {
17     public $validateCodeIdAttribute;
18     public $mobileAttribute;
19
20     public function validateAttribute($model, $attribute)
21     {
22         $value = $model->$attribute;
23         $validateCodeIdAttribute = $this->validateCodeIdAttribute;
24         $mobileAttribute = $this->mobileAttribute;
25         $validateCodeId = $model->$validateCodeIdAttribute;
26         $mobile = $model->$mobileAttribute;
27         $coreValidateCode = CoreValidateCode::findOne([
28             'id' => $validateCodeId,
29             'target' => $mobile,
30             'code' => $value,
31             'is_validated' => CoreValidateCode::IS_VALIDATED_FALSE,
32         ]);
33         if (!$coreValidateCode) {
34             $model->addError($attribute, "{$model->getAttributeLabel($attribute)}错误。");
35         } else {
36             $coreValidateCode->is_validated = CoreValidateCode::IS_VALIDATED_TRUE;
37             $coreValidateCode->save();
38         }
39     }
40 }