RangeValidator.php 3.14 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 41 42 43 44 45 46 47 48 49
	/**
	 * Initializes the validator.
	 * @throws InvalidConfigException if [[range]] is not set.
	 */
	public function init()
	{
		parent::init();
		if (!is_array($this->range)) {
			throw new InvalidConfigException('The "range" property must be set.');
		}
Qiang Xue committed
50
		if ($this->message === null) {
51
			$this->message = Yii::t('yii', '{attribute} is invalid.');
Qiang Xue committed
52
		}
Qiang Xue committed
53 54
	}

w  
Qiang Xue committed
55 56 57
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
58
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
59 60
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
61
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
62 63
	{
		$value = $object->$attribute;
Qiang Xue committed
64
		if (!$this->validateValue($value)) {
Qiang Xue committed
65
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
66 67 68
		}
	}

Qiang Xue committed
69 70 71 72 73 74 75 76 77 78 79
	/**
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
	 */
	public function validateValue($value)
	{
		return !$this->not && in_array($value, $this->range, $this->strict)
			|| $this->not && !in_array($value, $this->range, $this->strict);
	}

w  
Qiang Xue committed
80 81
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
82
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
83
	 * @param string $attribute the name of the attribute to be validated.
84 85
	 * @param \yii\base\View $view the view object that is going to be used to render views or view files
	 * containing a model form with this validator applied.
w  
Qiang Xue committed
86 87
	 * @return string the client-side validation script.
	 */
88
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
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
		}
Qiang Xue committed
94 95 96 97 98 99 100 101 102 103 104
		$options = array(
			'range' => $range,
			'not' => $this->not,
			'message' => Html::encode(strtr($this->message, array(
				'{attribute}' => $object->getAttributeLabel($attribute),
				'{value}' => $object->$attribute,
			))),
		);
		if ($this->skipOnEmpty) {
			$options['skipOnEmpty'] = 1;
		}
w  
Qiang Xue committed
105

106
		$view->registerAssetBundle('yii/validation');
Qiang Xue committed
107
		return 'yii.validation.range(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
108 109
	}
}