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