RangeValidator.php 2.12 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
{
	/**
	 * @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,
w  
Qiang Xue committed
35
	 * the attribute value should NOT be among the list of values defined via [[range]].
w  
Qiang Xue committed
36
	 **/
resurtm committed
37
	public $not = false;
w  
Qiang Xue committed
38

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

w  
Qiang Xue committed
53
	/**
Qiang Xue committed
54
	 * @inheritdoc
w  
Qiang Xue committed
55
	 */
Qiang Xue committed
56
	protected function validateValue($value)
w  
Qiang Xue committed
57
	{
Qiang Xue committed
58
		$valid = !$this->not && in_array($value, $this->range, $this->strict)
Qiang Xue committed
59
			|| $this->not && !in_array($value, $this->range, $this->strict);
Qiang Xue committed
60
		return $valid ? null : [$this->message, []];
Qiang Xue committed
61 62
	}

w  
Qiang Xue committed
63
	/**
Qiang Xue committed
64
	 * @inheritdoc
w  
Qiang Xue committed
65
	 */
66
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
67
	{
Alexander Makarov committed
68
		$range = [];
w  
Qiang Xue committed
69
		foreach ($this->range as $value) {
w  
Qiang Xue committed
70
			$range[] = (string)$value;
w  
Qiang Xue committed
71
		}
Alexander Makarov committed
72
		$options = [
Qiang Xue committed
73 74
			'range' => $range,
			'not' => $this->not,
75
			'message' => Yii::$app->getI18n()->format($this->message, [
76
				'attribute' => $object->getAttributeLabel($attribute),
77
			], Yii::$app->language),
Alexander Makarov committed
78
		];
Qiang Xue committed
79 80 81
		if ($this->skipOnEmpty) {
			$options['skipOnEmpty'] = 1;
		}
w  
Qiang Xue committed
82

83
		ValidationAsset::register($view);
Qiang Xue committed
84
		return 'yii.validation.range(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
85 86
	}
}