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\jquery\ValidationAsset; |
11
|
|
|
use yii\validators\client\ClientValidator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* StringValidator composes client-side validation code from [[\yii\validators\StringValidator]]. |
15
|
|
|
* |
16
|
|
|
* @see \yii\validators\StringValidator |
17
|
|
|
* @see ValidationAsset |
18
|
|
|
* |
19
|
|
|
* @author Paul Klimov <[email protected]> |
20
|
|
|
* @since 2.1.0 |
21
|
|
|
*/ |
22
|
|
|
class StringValidator extends ClientValidator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
public function build($validator, $model, $attribute, $view) |
28
|
|
|
{ |
29
|
|
|
ValidationAsset::register($view); |
30
|
|
|
$options = $this->getClientOptions($validator, $model, $attribute); |
|
|
|
|
31
|
|
|
return 'yii.validation.string(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the client-side validation options. |
36
|
|
|
* @param \yii\validators\StringValidator $validator the server-side validator. |
37
|
|
|
* @param \yii\base\Model $model the model being validated |
38
|
|
|
* @param string $attribute the attribute name being validated |
39
|
|
|
* @return array the client-side validation options |
40
|
|
|
*/ |
41
|
|
|
public function getClientOptions($validator, $model, $attribute) |
42
|
|
|
{ |
43
|
|
|
$label = $model->getAttributeLabel($attribute); |
44
|
|
|
|
45
|
|
|
$options = [ |
46
|
|
|
'message' => $validator->formatMessage($validator->message, [ |
47
|
|
|
'attribute' => $label, |
48
|
|
|
]), |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
if ($validator->min !== null) { |
52
|
|
|
$options['min'] = $validator->min; |
53
|
|
|
$options['tooShort'] = $validator->formatMessage($validator->tooShort, [ |
54
|
|
|
'attribute' => $label, |
55
|
|
|
'min' => $validator->min, |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
if ($validator->max !== null) { |
59
|
|
|
$options['max'] = $validator->max; |
60
|
|
|
$options['tooLong'] = $validator->formatMessage($validator->tooLong, [ |
61
|
|
|
'attribute' => $label, |
62
|
|
|
'max' => $validator->max, |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
if ($validator->length !== null) { |
66
|
|
|
$options['is'] = $validator->length; |
67
|
|
|
$options['notEqual'] = $validator->formatMessage($validator->notEqual, [ |
68
|
|
|
'attribute' => $label, |
69
|
|
|
'length' => $validator->length, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
if ($validator->skipOnEmpty) { |
73
|
|
|
$options['skipOnEmpty'] = 1; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $options; |
77
|
|
|
} |
78
|
|
|
} |
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.