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
|
|
|
* RangeValidator composes client-side validation code from [[\yii\validators\RangeValidator]]. |
15
|
|
|
* |
16
|
|
|
* @see \yii\validators\RangeValidator |
17
|
|
|
* @see ValidationAsset |
18
|
|
|
* |
19
|
|
|
* @author Paul Klimov <[email protected]> |
20
|
|
|
* @since 2.1.0 |
21
|
|
|
*/ |
22
|
|
|
class RangeValidator extends ClientValidator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
public function build($validator, $model, $attribute, $view) |
28
|
|
|
{ |
29
|
|
|
/* @var $validator \yii\validators\RangeValidator */ |
30
|
|
|
if ($validator->range instanceof \Closure) { |
31
|
|
|
$validator->range = call_user_func($validator->range, $model, $attribute); |
32
|
|
|
} |
33
|
|
|
ValidationAsset::register($view); |
34
|
|
|
$options = $validator->getClientOptions($model, $attribute); |
|
|
|
|
35
|
|
|
return 'yii.validation.range(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the client-side validation options. |
40
|
|
|
* @param \yii\validators\RangeValidator $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
|
|
|
$range = []; |
48
|
|
|
foreach ($validator->range as $value) { |
|
|
|
|
49
|
|
|
$range[] = (string) $value; |
50
|
|
|
} |
51
|
|
|
$options = [ |
52
|
|
|
'range' => $range, |
53
|
|
|
'not' => $validator->not, |
54
|
|
|
'message' => $validator->formatMessage($validator->message, [ |
55
|
|
|
'attribute' => $model->getAttributeLabel($attribute), |
56
|
|
|
]), |
57
|
|
|
]; |
58
|
|
|
if ($validator->skipOnEmpty) { |
59
|
|
|
$options['skipOnEmpty'] = 1; |
60
|
|
|
} |
61
|
|
|
if ($validator->allowArray) { |
62
|
|
|
$options['allowArray'] = 1; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $options; |
66
|
|
|
} |
67
|
|
|
} |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: