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; |
11
|
|
|
use yii\helpers\Html; |
12
|
|
|
use yii\helpers\Json; |
13
|
|
|
use yii\jquery\ValidationAsset; |
14
|
|
|
use yii\validators\client\ClientValidator; |
15
|
|
|
use yii\web\JsExpression; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* IpValidator composes client-side validation code from [[\yii\validators\IpValidator]]. |
19
|
|
|
* |
20
|
|
|
* @see \yii\validators\IpValidator |
21
|
|
|
* @see ValidationAsset |
22
|
|
|
* |
23
|
|
|
* @author Paul Klimov <[email protected]> |
24
|
|
|
* @since 2.1.0 |
25
|
|
|
*/ |
26
|
|
|
class IpValidator extends ClientValidator |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function build($validator, $model, $attribute, $view) |
32
|
|
|
{ |
33
|
|
|
ValidationAsset::register($view); |
34
|
|
|
$options = $this->getClientOptions($validator, $model, $attribute); |
|
|
|
|
35
|
|
|
return 'yii.validation.ip(value, messages, ' . Json::htmlEncode($options) . ');'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the client-side validation options. |
40
|
|
|
* @param \yii\validators\IpValidator $validator the server-side validator. |
41
|
|
|
* @param \yii\base\Model $model the model being validated |
42
|
|
|
* @param string $attribute the attribute name being validated |
43
|
|
|
* @return array the client-side validation options |
44
|
|
|
*/ |
45
|
|
|
public function getClientOptions($validator, $model, $attribute) |
46
|
|
|
{ |
47
|
|
|
$messages = [ |
48
|
|
|
'ipv6NotAllowed' => $validator->ipv6NotAllowed, |
49
|
|
|
'ipv4NotAllowed' => $validator->ipv4NotAllowed, |
50
|
|
|
'message' => $validator->message, |
51
|
|
|
'noSubnet' => $validator->noSubnet, |
52
|
|
|
'hasSubnet' => $validator->hasSubnet, |
53
|
|
|
]; |
54
|
|
|
foreach ($messages as &$message) { |
55
|
|
|
$message = $validator->formatMessage($message, [ |
56
|
|
|
'attribute' => $model->getAttributeLabel($attribute), |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$options = [ |
61
|
|
|
'ipv4Pattern' => new JsExpression(Html::escapeJsRegularExpression($validator->ipv4Pattern)), |
62
|
|
|
'ipv6Pattern' => new JsExpression(Html::escapeJsRegularExpression($validator->ipv6Pattern)), |
63
|
|
|
'messages' => $messages, |
64
|
|
|
'ipv4' => (bool) $validator->ipv4, |
65
|
|
|
'ipv6' => (bool) $validator->ipv6, |
66
|
|
|
'ipParsePattern' => new JsExpression(Html::escapeJsRegularExpression($validator->getIpParsePattern())), |
67
|
|
|
'negation' => $validator->negation, |
68
|
|
|
'subnet' => $validator->subnet, |
69
|
|
|
]; |
70
|
|
|
if ($validator->skipOnEmpty) { |
71
|
|
|
$options['skipOnEmpty'] = 1; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $options; |
75
|
|
|
} |
76
|
|
|
} |
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.