Browse Source

HKPhoneNum

master
kuangweijian 3 years ago
parent
commit
5960f9a544
  1. 2
      examples/user-register.php
  2. 18
      src/validators/HKPhoneNum.php

2
examples/user-register.php

@ -10,7 +10,7 @@
$inputDate=[ $inputDate=[
"name" => "kuangweijian", "name" => "kuangweijian",
"email" => "12345678@qq.com", "email" => "12345678@qq.com",
"hkPhoneNum"=> "8123456781234567",
"hkPhoneNum"=> "612345678",
"password" => "1234567890", "password" => "1234567890",
"pwdConfirm"=> "1234567890", "pwdConfirm"=> "1234567890",
]; ];

18
src/validators/HKPhoneNum.php

@ -1,6 +1,7 @@
<?php <?php
namespace wiggins\myProject\validators; namespace wiggins\myProject\validators;
use E;
use yii\validators\Validator; use yii\validators\Validator;
use Yii; use Yii;
@ -23,21 +24,24 @@
{ {
parent::init(); parent::init();
if ($this->message === null) { if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute}:{value} is not a valid hongkong phone number.');
$this->message = Yii::t('yii', '{attribute}({value})不是正确的香港电话号码.');
} }
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateAttribute($model, $attribute)
protected function validateValue($value)
{ {
echo "test";
if(!preg_match($this->pattern, $model->$attribute))
{
return false;
echo __FUNCTION__.PHP_EOL;
if(!is_string($value)){
$valid=false;
}elseif(!preg_match($this->pattern, $value)){
$valid=false;
}else{
$valid=true;
} }
return true;
return $valid ? null : [$this->message, []];
} }
} }
?> ?>
Loading…
Cancel
Save