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

RequiredValidator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 47.37%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 3
dl 0
loc 104
ccs 18
cts 38
cp 0.4737
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 3
B validateValue() 0 17 12
A clientValidateAttribute() 0 7 1
A getClientOptions() 0 21 3
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
use Yii;
11
12
/**
13
 * RequiredValidator validates that the specified attribute does not have null or empty value.
14
 *
15
 * @author Qiang Xue <[email protected]>
16
 * @since 2.0
17
 */
18
class RequiredValidator extends Validator
19
{
20
    /**
21
     * @var bool whether to skip this validator if the value being validated is empty.
22
     */
23
    public $skipOnEmpty = false;
24
    /**
25
     * @var mixed the desired value that the attribute must have.
26
     * If this is null, the validator will validate that the specified attribute is not empty.
27
     * If this is set as a value that is not null, the validator will validate that
28
     * the attribute has a value that is the same as this property value.
29
     * Defaults to null.
30
     * @see strict
31
     */
32
    public $requiredValue;
33
    /**
34
     * @var bool whether the comparison between the attribute value and [[requiredValue]] is strict.
35
     * When this is true, both the values and types must match.
36
     * Defaults to false, meaning only the values need to match.
37
     * Note that when [[requiredValue]] is null, if this property is true, the validator will check
38
     * if the attribute value is null; If this property is false, the validator will call [[isEmpty]]
39
     * to check if the attribute value is empty.
40
     */
41
    public $strict = false;
42
    /**
43
     * @var string the user-defined error message. It may contain the following placeholders which
44
     * will be replaced accordingly by the validator:
45
     *
46
     * - `{attribute}`: the label of the attribute being validated
47
     * - `{value}`: the value of the attribute being validated
48
     * - `{requiredValue}`: the value of [[requiredValue]]
49
     */
50
    public $message;
51
52
53
    /**
54
     * @inheritdoc
55
     */
56 26
    public function init()
57
    {
58 26
        parent::init();
59 26
        if ($this->message === null) {
60 26
            $this->message = $this->requiredValue === null ? Yii::t('yii', '{attribute} cannot be blank.')
61 26
                : Yii::t('yii', '{attribute} must be "{requiredValue}".');
62 26
        }
63 26
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 11
    protected function validateValue($value)
69
    {
70 11
        if ($this->requiredValue === null) {
71 10
            if ($this->strict && $value !== null || !$this->strict && !$this->isEmpty(is_string($value) ? trim($value) : $value)) {
72 9
                return null;
73
            }
74 6
        } elseif (!$this->strict && $value == $this->requiredValue || $this->strict && $value === $this->requiredValue) {
75 2
            return null;
76
        }
77 6
        if ($this->requiredValue === null) {
78 5
            return [$this->message, []];
79
        } else {
80 2
            return [$this->message, [
81 2
                'requiredValue' => $this->requiredValue,
82 2
            ]];
83
        }
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function clientValidateAttribute($model, $attribute, $view)
90
    {
91
        ValidationAsset::register($view);
92
        $options = $this->getClientOptions($model, $attribute);
93
94
        return 'yii.validation.required(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function getClientOptions($model, $attribute)
101
    {
102
        $options = [];
103
        if ($this->requiredValue !== null) {
104
            $options['message'] = Yii::$app->getI18n()->format($this->message, [
105
                'requiredValue' => $this->requiredValue,
106
            ], Yii::$app->language);
107
            $options['requiredValue'] = $this->requiredValue;
108
        } else {
109
            $options['message'] = $this->message;
110
        }
111
        if ($this->strict) {
112
            $options['strict'] = 1;
113
        }
114
115
        $options['message'] = Yii::$app->getI18n()->format($options['message'], [
116
            'attribute' => $model->getAttributeLabel($attribute),
117
        ], Yii::$app->language);
118
119
        return $options;
120
    }
121
}
122