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\jquery\validators\client; |
9
|
|
|
|
10
|
|
|
use yii\helpers\Html; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
use yii\jquery\ValidationAsset; |
13
|
|
|
use yii\validators\client\ClientValidator; |
14
|
|
|
use yii\web\JsExpression; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* RegularExpressionValidator composes client-side validation code from [[\yii\validators\RegularExpressionValidator]]. |
18
|
|
|
* |
19
|
|
|
* @see \yii\validators\RegularExpressionValidator |
20
|
|
|
* @see ValidationAsset |
21
|
|
|
* |
22
|
|
|
* @author Paul Klimov <[email protected]> |
23
|
|
|
* @since 2.1.0 |
24
|
|
|
*/ |
25
|
|
|
class RegularExpressionValidator extends ClientValidator |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function build($validator, $model, $attribute, $view) |
31
|
|
|
{ |
32
|
|
|
ValidationAsset::register($view); |
33
|
|
|
$options = $this->getClientOptions($validator, $model, $attribute); |
|
|
|
|
34
|
|
|
return 'yii.validation.regularExpression(value, messages, ' . Json::htmlEncode($options) . ');'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns the client-side validation options. |
39
|
|
|
* @param \yii\validators\RegularExpressionValidator $validator the server-side validator. |
40
|
|
|
* @param \yii\base\Model $model the model being validated |
41
|
|
|
* @param string $attribute the attribute name being validated |
42
|
|
|
* @return array the client-side validation options |
43
|
|
|
*/ |
44
|
|
|
public function getClientOptions($validator, $model, $attribute) |
45
|
|
|
{ |
46
|
|
|
$pattern = Html::escapeJsRegularExpression($validator->pattern); |
47
|
|
|
|
48
|
|
|
$options = [ |
49
|
|
|
'pattern' => new JsExpression($pattern), |
50
|
|
|
'not' => $validator->not, |
51
|
|
|
'message' => $validator->formatMessage($validator->message, [ |
52
|
|
|
'attribute' => $model->getAttributeLabel($attribute), |
53
|
|
|
]), |
54
|
|
|
]; |
55
|
|
|
if ($validator->skipOnEmpty) { |
56
|
|
|
$options['skipOnEmpty'] = 1; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $options; |
60
|
|
|
} |
61
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.