RangeValidator.php 2.81 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8
namespace yii\validators;
Qiang Xue committed
9 10

use Yii;
Qiang Xue committed
11
use yii\base\InvalidConfigException;
w  
Qiang Xue committed
12

w  
Qiang Xue committed
13
/**
w  
Qiang Xue committed
14 15 16 17 18
 * RangeValidator validates that the attribute value is among a list of values.
 *
 * The range can be specified via the [[range]] property.
 * If the [[not]] property is set true, the validator will ensure the attribute value
 * is NOT among the specified range.
w  
Qiang Xue committed
19 20
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
21
 * @since 2.0
w  
Qiang Xue committed
22
 */
w  
Qiang Xue committed
23
class RangeValidator extends Validator
w  
Qiang Xue committed
24
{
25 26 27 28 29 30 31 32 33 34 35
    /**
     * @var array list of valid values that the attribute value should be among
     */
    public $range;
    /**
     * @var boolean whether the comparison is strict (both type and value must be the same)
     */
    public $strict = false;
    /**
     * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
     * the attribute value should NOT be among the list of values defined via [[range]].
36
     */
37
    public $not = false;
38 39 40 41
    /**
     * @var boolean whether to allow array type attribute.
     */
    public $allowArray = false;
w  
Qiang Xue committed
42

43

44 45 46 47 48 49 50 51 52 53 54 55 56
    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        if (!is_array($this->range)) {
            throw new InvalidConfigException('The "range" property must be set.');
        }
        if ($this->message === null) {
            $this->message = Yii::t('yii', '{attribute} is invalid.');
        }
    }
Qiang Xue committed
57

58 59 60 61 62
    /**
     * @inheritdoc
     */
    protected function validateValue($value)
    {
63 64 65 66
        if (!$this->allowArray && is_array($value)) {
            return [$this->message, []];
        }

Qiang Xue committed
67
        $in = true;
68

69
        foreach ((is_array($value) ? $value : [$value]) as $v) {
Qiang Xue committed
70 71
            if (!in_array($v, $this->range, $this->strict)) {
                $in = false;
72 73 74
                break;
            }
        }
Qiang Xue committed
75

76
        return $this->not !== $in ? null : [$this->message, []];
77
    }
w  
Qiang Xue committed
78

79 80 81
    /**
     * @inheritdoc
     */
Qiang Xue committed
82
    public function clientValidateAttribute($model, $attribute, $view)
83 84 85 86 87 88 89 90 91
    {
        $range = [];
        foreach ($this->range as $value) {
            $range[] = (string) $value;
        }
        $options = [
            'range' => $range,
            'not' => $this->not,
            'message' => Yii::$app->getI18n()->format($this->message, [
Qiang Xue committed
92
                'attribute' => $model->getAttributeLabel($attribute),
93 94 95 96 97
            ], Yii::$app->language),
        ];
        if ($this->skipOnEmpty) {
            $options['skipOnEmpty'] = 1;
        }
98 99 100
        if ($this->allowArray) {
            $options['allowArray'] = 1;
        }
101 102 103

        ValidationAsset::register($view);

104
        return 'yii.validation.range(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
105
    }
w  
Qiang Xue committed
106
}