-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
InlineValidator.php
101 lines (94 loc) · 3.27 KB
/
InlineValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\validators;
/**
* InlineValidator represents a validator which is defined as a method in the object being validated.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class InlineValidator extends Validator
{
/**
* @var string|callable an anonymous function or the name of a model class method that will be
* called to perform the actual validation. The signature of the method should be like the following:
*
* ```php
* function (string $attribute, mixed $params, InlineValidator $validator, mixed $current): bool {
* }
* ```
*
* - `$attribute` is the name of the attribute to be validated
* - `$params` contains the value of [[params]] that you specify when declaring the inline validation rule
* - `$validator` is a reference to related [[InlineValidator]] object. This parameter is available since version 2.0.11
* - `$current` is the attribute value. This parameter is available since version 2.0.36
*/
public $method;
/**
* @var mixed additional parameters that are passed to the validation method
*/
public $params;
/**
* @var string|\Closure an anonymous function or the name of a model class method that returns the client validation code.
* The signature of the method should be like the following:
*
* ```php
* function (string $attribute, mixed $params, InlineValidator $validator, mixed $current, View $view): string
* {
* // $view->registerJs('JS validation function');
* // or \app\assets\ValidationAsset::register($view);
* return "calling JS validation function";
* }
* ```
*
* Please refer to [[clientValidateAttribute()]] and [guide](guide:input-validation#client-side-validation) for details on how
* to return client validation code.
*/
public $clientValidate;
/**
* @var mixed the value of attribute being currently validated.
* @since 2.0.36
*/
public $current;
/**
* {@inheritdoc}
*/
public function validateAttribute($model, $attribute)
{
$method = $this->method;
if (is_string($method)) {
$method = [$model, $method];
} elseif ($method instanceof \Closure) {
$method = $this->method->bindTo($model);
}
$current = $this->current;
if ($current === null) {
$current = $model->$attribute;
}
$method($attribute, $this->params, $this, $current);
}
/**
* {@inheritdoc}
*/
public function clientValidateAttribute($model, $attribute, $view)
{
if ($this->clientValidate !== null) {
$method = $this->clientValidate;
if (is_string($method)) {
$method = [$model, $method];
} elseif ($method instanceof \Closure) {
$method = $method->bindTo($model);
}
$current = $this->current;
if ($current === null) {
$current = $model->$attribute;
}
return $method($attribute, $this->params, $this, $current, $view);
}
return null;
}
}