30 lines
724 B
PHP
30 lines
724 B
PHP
<?php
|
|
|
|
namespace common\validators;
|
|
|
|
use yii\validators\Validator;
|
|
|
|
class MobieValidator extends Validator
|
|
{
|
|
private $_pattern = '/^1[3|4|5|6|8|7|9]\d{9}$/';
|
|
|
|
private $_message = '手机号格式不对';
|
|
public function validate($value, &$error = null)
|
|
{
|
|
$valid = false;
|
|
if (preg_match($this->_pattern, $value, $arr)) {
|
|
$valid = true;
|
|
}
|
|
return $valid ? null : [$this->_message, []];
|
|
}
|
|
|
|
public function validateAttribute($model, $attribute)
|
|
{
|
|
$valid = false;
|
|
if (preg_match($this->_pattern, $model->$attribute, $arr)) {
|
|
$valid = true;
|
|
}
|
|
$valid ?: $this->addError($model, $attribute, $this->_message);
|
|
}
|
|
}
|