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\validators; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* InlineValidator represents a validator which is defined as a method in the object being validated. |
12
|
|
|
* |
13
|
|
|
* The validation method must have the following signature: |
14
|
|
|
* |
15
|
|
|
* ```php |
16
|
|
|
* function foo($attribute, $params, $validator) |
17
|
|
|
* ``` |
18
|
|
|
* |
19
|
|
|
* where `$attribute` refers to the name of the attribute being validated, while `$params` is an array representing the |
20
|
|
|
* additional parameters supplied in the validation rule. Parameter `$validator` refers to the related |
21
|
|
|
* [[InlineValidator]] object and is available since version 2.0.11. |
22
|
|
|
* |
23
|
|
|
* @author Qiang Xue <[email protected]> |
24
|
|
|
* @since 2.0 |
25
|
|
|
*/ |
26
|
|
|
class InlineValidator extends Validator |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string|\Closure an anonymous function or the name of a model class method that will be |
30
|
|
|
* called to perform the actual validation. The signature of the method should be like the following: |
31
|
|
|
* |
32
|
|
|
* ```php |
33
|
|
|
* function foo($attribute, $params, $validator) |
34
|
|
|
* ``` |
35
|
|
|
* |
36
|
|
|
* - `$attribute` is the name of the attribute to be validated; |
37
|
|
|
* - `$params` contains the value of [[params]] that you specify when declaring the inline validation rule; |
38
|
|
|
* - `$validator` is a reference to related [[InlineValidator]] object. |
39
|
|
|
*/ |
40
|
|
|
public $method; |
41
|
|
|
/** |
42
|
|
|
* @var mixed additional parameters that are passed to the validation method |
43
|
|
|
*/ |
44
|
|
|
public $params; |
45
|
|
|
/** |
46
|
|
|
* @var string|\Closure an anonymous function or the name of a model class method that returns the client validation code. |
47
|
|
|
* The signature of the method should be like the following: |
48
|
|
|
* |
49
|
|
|
* ```php |
50
|
|
|
* function foo($attribute, $params, $validator) |
51
|
|
|
* { |
52
|
|
|
* return "javascript"; |
53
|
|
|
* } |
54
|
|
|
* ``` |
55
|
|
|
* |
56
|
|
|
* where `$attribute` refers to the attribute name to be validated. |
57
|
|
|
* |
58
|
|
|
* Please refer to [[clientValidateAttribute()]] for details on how to return client validation code. |
59
|
|
|
*/ |
60
|
|
|
public $clientValidate; |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
1 |
|
public function validateAttribute($model, $attribute) |
67
|
|
|
{ |
68
|
1 |
|
$method = $this->method; |
69
|
1 |
|
if (is_string($method)) { |
70
|
1 |
|
$method = [$model, $method]; |
71
|
1 |
|
} |
72
|
1 |
|
call_user_func($method, $attribute, $this->params, $this); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
1 |
|
public function clientValidateAttribute($model, $attribute, $view) |
79
|
|
|
{ |
80
|
1 |
|
if ($this->clientValidate !== null) { |
81
|
1 |
|
$method = $this->clientValidate; |
82
|
1 |
|
if (is_string($method)) { |
83
|
1 |
|
$method = [$model, $method]; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
1 |
|
return call_user_func($method, $attribute, $this->params, $this); |
87
|
|
|
} else { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|