Completed
Push — 2.1 ( 75349f...bf116e )
by Alexander
29:27
created

RequiredValidator::getClientOptions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 12
cp 0
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 2
crap 12
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 29
    public function init()
57
    {
58 29
        parent::init();
59 29
        if ($this->message === null) {
60 29
            $this->message = $this->requiredValue === null ? Yii::t('yii', '{attribute} cannot be blank.')
61 2
                : Yii::t('yii', '{attribute} must be "{requiredValue}".');
62
        }
63 29
    }
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 10
                return null;
73
            }
74 2
        } 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
        }
80
81 2
        return [$this->message, [
82 2
            'requiredValue' => $this->requiredValue,
83
        ]];
84
    }
85
}
86