Files
xk-api-yii/common/validators/MobileValidator.php
2024-09-19 11:32:39 +08:00

36 lines
814 B
PHP

<?php
namespace common\validators;
use yii\validators\Validator;
/**
* Created by PhpStorm.
* User: chatfeed
* Date: 2018/5/28
* Time: 下午2:25
*/
class MobileValidator 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);
}
}