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
|
|
|
* FileValidator composes client-side validation code from [[\yii\validators\FileValidator]]. |
19
|
|
|
* |
20
|
|
|
* @see \yii\validators\FileValidator |
21
|
|
|
* @see ValidationAsset |
22
|
|
|
* |
23
|
|
|
* @author Paul Klimov <[email protected]> |
24
|
|
|
* @since 2.1.0 |
25
|
|
|
*/ |
26
|
|
|
class FileValidator 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.file(attribute, messages, ' . Json::encode($options) . ');'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the client-side validation options. |
40
|
|
|
* @param \yii\validators\FileValidator $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
|
|
|
$label = $model->getAttributeLabel($attribute); |
48
|
|
|
|
49
|
|
|
$options = []; |
50
|
|
|
if ($validator->message !== null) { |
51
|
|
|
$options['message'] = $validator->formatMessage($validator->message, [ |
52
|
|
|
'attribute' => $label, |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$options['skipOnEmpty'] = $validator->skipOnEmpty; |
57
|
|
|
|
58
|
|
|
if (!$validator->skipOnEmpty) { |
59
|
|
|
$options['uploadRequired'] = $validator->formatMessage($validator->uploadRequired, [ |
60
|
|
|
'attribute' => $label, |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($validator->mimeTypes !== null) { |
65
|
|
|
$mimeTypes = []; |
66
|
|
|
foreach ($validator->mimeTypes as $mimeType) { |
|
|
|
|
67
|
|
|
$mimeTypes[] = new JsExpression(Html::escapeJsRegularExpression($validator->buildMimeTypeRegexp($mimeType))); |
68
|
|
|
} |
69
|
|
|
$options['mimeTypes'] = $mimeTypes; |
70
|
|
|
$options['wrongMimeType'] = $validator->formatMessage($validator->wrongMimeType, [ |
71
|
|
|
'attribute' => $label, |
72
|
|
|
'mimeTypes' => implode(', ', $validator->mimeTypes), |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($validator->extensions !== null) { |
77
|
|
|
$options['extensions'] = $validator->extensions; |
78
|
|
|
$options['wrongExtension'] = $validator->formatMessage($validator->wrongExtension, [ |
79
|
|
|
'attribute' => $label, |
80
|
|
|
'extensions' => implode(', ', $validator->extensions), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($validator->minSize !== null) { |
85
|
|
|
$options['minSize'] = $validator->minSize; |
86
|
|
|
$options['tooSmall'] = $validator->formatMessage($validator->tooSmall, [ |
87
|
|
|
'attribute' => $label, |
88
|
|
|
'limit' => $validator->minSize, |
89
|
|
|
'formattedLimit' => Yii::$app->formatter->asShortSize($validator->minSize), |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($validator->maxSize !== null) { |
94
|
|
|
$options['maxSize'] = $validator->maxSize; |
95
|
|
|
$options['tooBig'] = $validator->formatMessage($validator->tooBig, [ |
96
|
|
|
'attribute' => $label, |
97
|
|
|
'limit' => $validator->getSizeLimit(), |
98
|
|
|
'formattedLimit' => Yii::$app->formatter->asShortSize($validator->getSizeLimit()), |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($validator->maxFiles !== null) { |
103
|
|
|
$options['maxFiles'] = $validator->maxFiles; |
104
|
|
|
$options['tooMany'] = $validator->formatMessage($validator->tooMany, [ |
105
|
|
|
'attribute' => $label, |
106
|
|
|
'limit' => $validator->maxFiles, |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $options; |
111
|
|
|
} |
112
|
|
|
} |
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.