初始化仓库
This commit is contained in:
35
common/validators/MobileValidator.php
Normal file
35
common/validators/MobileValidator.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user