RangeValidator.php 3.48 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * RangeValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10
namespace yii\validators;
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 36 37 38 39
{
	/**
	 * @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 the attribute value can be null or empty. Defaults to true,
	 * meaning that if the attribute is empty, it is considered valid.
	 */
	public $allowEmpty = true;
	/**
	 * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
w  
Qiang Xue committed
40
	 * the attribute value should NOT be among the list of values defined via [[range]].
w  
Qiang Xue committed
41 42 43 44 45 46
	 **/
 	public $not = false;

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
47
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
48
	 * @param string $attribute the attribute being validated
Qiang Xue committed
49
	 * @throws InvalidConfigException if the "range" property is not an array
w  
Qiang Xue committed
50
	 */
w  
Qiang Xue committed
51
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
52 53
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
54
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
55
			return;
w  
Qiang Xue committed
56 57
		}
		if (!is_array($this->range)) {
Qiang Xue committed
58
			throw new InvalidConfigException('The "range" property must be specified as an array.');
w  
Qiang Xue committed
59 60
		}
		if (!$this->not && !in_array($value, $this->range, $this->strict)) {
Alexander Makarov committed
61
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} should be in the list.');
w  
Qiang Xue committed
62
			$this->addError($object, $attribute, $message);
Qiang Xue committed
63
		} elseif ($this->not && in_array($value, $this->range, $this->strict)) {
Alexander Makarov committed
64
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} should NOT be in the list.');
w  
Qiang Xue committed
65 66 67 68 69 70
			$this->addError($object, $attribute, $message);
		}
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
71
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
72 73
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
Qiang Xue committed
74
	 * @throws InvalidConfigException if the "range" property is not an array
w  
Qiang Xue committed
75 76 77
	 */
	public function clientValidateAttribute($object, $attribute)
	{
w  
Qiang Xue committed
78
		if (!is_array($this->range)) {
Qiang Xue committed
79
			throw new InvalidConfigException('The "range" property must be specified as an array.');
w  
Qiang Xue committed
80
		}
w  
Qiang Xue committed
81

w  
Qiang Xue committed
82
		if (($message = $this->message) === null) {
Alexander Makarov committed
83
			$message = $this->not ? \Yii::t('yii', '{attribute} should NOT be in the list.') : \Yii::t('yii', '{attribute} should be in the list.');
w  
Qiang Xue committed
84
		}
w  
Qiang Xue committed
85 86
		$message = strtr($message, array(
			'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
87
			'{value}' => $object->$attribute,
w  
Qiang Xue committed
88 89 90
		));

		$range = array();
w  
Qiang Xue committed
91
		foreach ($this->range as $value) {
w  
Qiang Xue committed
92
			$range[] = (string)$value;
w  
Qiang Xue committed
93
		}
w  
Qiang Xue committed
94
		$range = json_encode($range);
w  
Qiang Xue committed
95 96

		return "
w  
Qiang Xue committed
97
if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? "$.inArray(value, $range)>=0" : "$.inArray(value, $range)<0") . ") {
w  
Qiang Xue committed
98
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
99 100 101 102
}
";
	}
}