1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\validators; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\helpers\Html; |
13
|
|
|
use yii\web\JsExpression; |
14
|
|
|
use yii\helpers\Json; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* RegularExpressionValidator validates that the attribute value matches the specified [[pattern]]. |
18
|
|
|
* |
19
|
|
|
* If the [[not]] property is set true, the validator will ensure the attribute value do NOT match the [[pattern]]. |
20
|
|
|
* |
21
|
|
|
* @author Qiang Xue <[email protected]> |
22
|
|
|
* @since 2.0 |
23
|
|
|
*/ |
24
|
|
|
class RegularExpressionValidator extends Validator |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string the regular expression to be matched with |
28
|
|
|
*/ |
29
|
|
|
public $pattern; |
30
|
|
|
/** |
31
|
|
|
* @var bool whether to invert the validation logic. Defaults to false. If set to true, |
32
|
|
|
* the regular expression defined via [[pattern]] should NOT match the attribute value. |
33
|
|
|
*/ |
34
|
|
|
public $not = false; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
4 |
|
public function init() |
41
|
|
|
{ |
42
|
4 |
|
parent::init(); |
43
|
4 |
|
if ($this->pattern === null) { |
44
|
1 |
|
throw new InvalidConfigException('The "pattern" property must be set.'); |
45
|
|
|
} |
46
|
3 |
|
if ($this->message === null) { |
47
|
3 |
|
$this->message = Yii::t('yii', '{attribute} is invalid.'); |
48
|
|
|
} |
49
|
3 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
2 |
|
protected function validateValue($value) |
55
|
|
|
{ |
56
|
2 |
|
$valid = !is_array($value) && |
57
|
2 |
|
(!$this->not && preg_match($this->pattern, $value) |
58
|
2 |
|
|| $this->not && !preg_match($this->pattern, $value)); |
59
|
|
|
|
60
|
2 |
|
return $valid ? null : [$this->message, []]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
|
|
public function clientValidateAttribute($model, $attribute, $view) |
67
|
|
|
{ |
68
|
|
|
ValidationAsset::register($view); |
69
|
|
|
$options = $this->getClientOptions($model, $attribute); |
70
|
|
|
|
71
|
|
|
return 'yii.validation.regularExpression(value, messages, ' . Json::htmlEncode($options) . ');'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
|
|
public function getClientOptions($model, $attribute) |
78
|
|
|
{ |
79
|
|
|
$pattern = Html::escapeJsRegularExpression($this->pattern); |
80
|
|
|
|
81
|
|
|
$options = [ |
82
|
|
|
'pattern' => new JsExpression($pattern), |
83
|
|
|
'not' => $this->not, |
84
|
|
|
'message' => $this->formatMessage($this->message, [ |
85
|
|
|
'attribute' => $model->getAttributeLabel($attribute), |
86
|
|
|
]), |
87
|
|
|
]; |
88
|
|
|
if ($this->skipOnEmpty) { |
89
|
|
|
$options['skipOnEmpty'] = 1; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $options; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|