Browse Source

HKPhoneNum

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

2
examples/user-register.php

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

20
src/validators/HKPhoneNum.php

@ -1,7 +1,8 @@
<?php
namespace wiggins\myProject\validators;
use yii\validators\Validator;
use E;
use yii\validators\Validator;
use Yii;
/**
@ -23,21 +24,24 @@
{
parent::init();
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}
*/
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