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;
Qiang Xue committed
12
use yii\helpers\Html;
w  
Qiang Xue committed
13

w  
Qiang Xue committed
14
/**
w  
Qiang Xue committed
15 16 17 18 19
 * 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
20 21
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
22
 * @since 2.0
w  
Qiang Xue committed
23
 */
w  
Qiang Xue committed
24
class RangeValidator extends Validator
w  
Qiang Xue committed
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,
w  
Qiang Xue committed
36
	 * the attribute value should NOT be among the list of values defined via [[range]].
w  
Qiang Xue committed
37
	 **/
resurtm committed
38
	public $not = false;
w  
Qiang Xue committed
39

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

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

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

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