Completed
Push — fix-numbervalidator-comma-deci... ( 08054b...a7f0a3 )
by Alexander
40:41 queued 37:41
created

InlineValidator::clientValidateAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 3
crap 3.0175
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