From fcf5b559b78743038b5f323857bedb36cd32152f Mon Sep 17 00:00:00 2001 From: kuangweijian <867034892@qq.com> Date: Tue, 29 Mar 2022 15:18:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0validateAttribute()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/user-register.php | 2 +- src/validators/HKPhoneNum.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/user-register.php b/examples/user-register.php index e185fba..e37fac0 100644 --- a/examples/user-register.php +++ b/examples/user-register.php @@ -10,7 +10,7 @@ $inputDate=[ "name" => "kuangweijian", "email" => "12345678@qq.com", - "hkPhoneNum"=> "012345678", + "hkPhoneNum"=> "", "password" => "1234567890", "pwdConfirm"=> "1234567890", ]; diff --git a/src/validators/HKPhoneNum.php b/src/validators/HKPhoneNum.php index 43591bc..8bee23f 100644 --- a/src/validators/HKPhoneNum.php +++ b/src/validators/HKPhoneNum.php @@ -28,6 +28,17 @@ use yii\validators\Validator; } } + /** + * {@inheritdoc} + */ + public function validateAttribute($model, $attribute) + { + $result = $this->validateValue($model->$attribute); + if (!empty($result)) { + $this->addError($model, $attribute, $result[0], $result[1]); + } + } + /** * {@inheritdoc} */ @@ -42,5 +53,6 @@ use yii\validators\Validator; } return $valid ? null : [$this->message, []]; } + } ?> \ No newline at end of file