RangeValidator.php 3.43 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/
w  
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\validators;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13 14 15 16 17
 * 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
18 19
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
20
 * @since 2.0
w  
Qiang Xue committed
21
 */
w  
Qiang Xue committed
22
class RangeValidator extends Validator
w  
Qiang Xue committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
{
	/**
	 * @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
39
	 * the attribute value should NOT be among the list of values defined via [[range]].
w  
Qiang Xue committed
40 41 42 43 44 45
	 **/
 	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
46
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
47
	 * @param string $attribute the attribute being validated
Alexander Makarov committed
48
	 * @throws \yii\base\Exception if the "range" property is not an array
w  
Qiang Xue committed
49
	 */
w  
Qiang Xue committed
50
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
51 52
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
53
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
54
			return;
w  
Qiang Xue committed
55 56 57 58 59
		}
		if (!is_array($this->range)) {
			throw new \yii\base\Exception('The "range" property must be specified as an array.');
		}
		if (!$this->not && !in_array($value, $this->range, $this->strict)) {
Alexander Makarov committed
60
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} should be in the list.');
w  
Qiang Xue committed
61
			$this->addError($object, $attribute, $message);
Qiang Xue committed
62
		} elseif ($this->not && in_array($value, $this->range, $this->strict)) {
Alexander Makarov committed
63
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} should NOT be in the list.');
w  
Qiang Xue committed
64 65 66 67 68 69
			$this->addError($object, $attribute, $message);
		}
	}

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

w  
Qiang Xue committed
81
		if (($message = $this->message) === null) {
Alexander Makarov committed
82
			$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
83
		}
w  
Qiang Xue committed
84 85
		$message = strtr($message, array(
			'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
86
			'{value}' => $object->$attribute,
w  
Qiang Xue committed
87 88 89
		));

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

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