CompareValidator.php 7.73 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * CompareValidator 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
 * CompareValidator compares the specified attribute value with another value and validates if they are equal.
w  
Qiang Xue committed
14 15
 *
 * The value being compared with can be another attribute value
w  
Qiang Xue committed
16 17
 * (specified via [[compareAttribute]]) or a constant (specified via
 * [[compareValue]]. When both are specified, the latter takes
w  
Qiang Xue committed
18 19 20 21
 * precedence. If neither is specified, the attribute will be compared
 * with another attribute whose name is by appending "_repeat" to the source
 * attribute name.
 *
w  
Qiang Xue committed
22
 * The comparison can be either [[strict]] or not.
w  
Qiang Xue committed
23
 *
w  
Qiang Xue committed
24 25
 * CompareValidator supports different comparison operators, specified
 * via the [[operator]] property.
w  
Qiang Xue committed
26 27
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
28
 * @since 2.0
w  
Qiang Xue committed
29
 */
w  
Qiang Xue committed
30
class CompareValidator extends Validator
w  
Qiang Xue committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
{
	/**
	 * @var string the name of the attribute to be compared with
	 */
	public $compareAttribute;
	/**
	 * @var string the constant value to be compared with
	 */
	public $compareValue;
	/**
	 * @var boolean whether the comparison is strict (both value and type must be the same.)
	 * Defaults to false.
	 */
	public $strict = false;
	/**
	 * @var boolean whether the attribute value can be null or empty. Defaults to false.
	 * If this is true, it means the attribute is considered valid when it is empty.
	 */
	public $allowEmpty = false;
	/**
	 * @var string the operator for comparison. Defaults to '='.
	 * The followings are valid operators:
	 * <ul>
w  
Qiang Xue committed
54
	 * <li>'=' or '==': validates to see if the two values are equal. If [[strict]] is true, the comparison
w  
Qiang Xue committed
55
	 * will be done in strict mode (i.e. checking value type as well).</li>
w  
Qiang Xue committed
56
	 * <li>'!=': validates to see if the two values are NOT equal. If [[strict]] is true, the comparison
w  
Qiang Xue committed
57 58 59 60 61 62 63 64 65 66 67 68
	 * will be done in strict mode (i.e. checking value type as well).</li>
	 * <li>'>': validates to see if the value being validated is greater than the value being compared with.</li>
	 * <li>'>=': validates to see if the value being validated is greater than or equal to the value being compared with.</li>
	 * <li>'<': validates to see if the value being validated is less than the value being compared with.</li>
	 * <li>'<=': validates to see if the value being validated is less than or equal to the value being compared with.</li>
	 * </ul>
	 */
	public $operator = '=';

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
69
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
70
	 * @param string $attribute the attribute being validated
Alexander Makarov committed
71
	 * @throws \yii\base\Exception if CompareValidator::operator is invalid
w  
Qiang Xue committed
72
	 */
w  
Qiang Xue committed
73
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
74 75
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
76
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
77
			return;
w  
Qiang Xue committed
78 79
		}
		if ($this->compareValue !== null) {
w  
Qiang Xue committed
80
			$compareTo = $compareValue = $this->compareValue;
Qiang Xue committed
81
		} else {
Alexander Makarov committed
82
			$compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute;
w  
Qiang Xue committed
83 84 85 86
			$compareValue = $object->$compareAttribute;
			$compareTo = $object->getAttributeLabel($compareAttribute);
		}

w  
Qiang Xue committed
87
		switch ($this->operator) {
w  
Qiang Xue committed
88 89
			case '=':
			case '==':
w  
Qiang Xue committed
90
				if (($this->strict && $value !== $compareValue) || (!$this->strict && $value != $compareValue)) {
Alexander Makarov committed
91
					$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be repeated exactly.');
w  
Qiang Xue committed
92 93 94 95
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo));
				}
				break;
			case '!=':
w  
Qiang Xue committed
96
				if (($this->strict && $value === $compareValue) || (!$this->strict && $value == $compareValue)) {
Alexander Makarov committed
97
					$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
w  
Qiang Xue committed
98 99 100 101
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
				}
				break;
			case '>':
w  
Qiang Xue committed
102
				if ($value <= $compareValue) {
Alexander Makarov committed
103
					$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be greater than "{compareValue}".');
w  
Qiang Xue committed
104 105 106 107
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
				}
				break;
			case '>=':
w  
Qiang Xue committed
108
				if ($value < $compareValue) {
Alexander Makarov committed
109
					$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".');
w  
Qiang Xue committed
110 111 112 113
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
				}
				break;
			case '<':
w  
Qiang Xue committed
114
				if ($value >= $compareValue) {
Alexander Makarov committed
115
					$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be less than "{compareValue}".');
w  
Qiang Xue committed
116 117 118 119
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
				}
				break;
			case '<=':
w  
Qiang Xue committed
120
				if ($value > $compareValue) {
Alexander Makarov committed
121
					$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".');
w  
Qiang Xue committed
122 123 124 125
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
				}
				break;
			default:
w  
Qiang Xue committed
126
				throw new \yii\base\Exception('Invalid operator "' . $this->operator . '".');
w  
Qiang Xue committed
127 128 129 130 131
		}
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
132
	 * @param \yii\base\Model $object the data object being validated
Alexander Makarov committed
133 134 135
	 * @param string $attribute the name of the attribute to be validated
	 * @return string the client-side validation script
	 * @throws \yii\base\Exception if CompareValidator::operator is invalid
w  
Qiang Xue committed
136 137 138
	 */
	public function clientValidateAttribute($object, $attribute)
	{
w  
Qiang Xue committed
139
		if ($this->compareValue !== null) {
w  
Qiang Xue committed
140
			$compareTo = $this->compareValue;
w  
Qiang Xue committed
141
			$compareValue = json_encode($this->compareValue);
Qiang Xue committed
142
		} else {
Alexander Makarov committed
143
			$compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute;
w  
Qiang Xue committed
144 145 146 147 148
			$compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()";
			$compareTo = $object->getAttributeLabel($compareAttribute);
		}

		$message = $this->message;
w  
Qiang Xue committed
149
		switch ($this->operator) {
w  
Qiang Xue committed
150 151
			case '=':
			case '==':
w  
Qiang Xue committed
152
				if ($message === null) {
Alexander Makarov committed
153
					$message = \Yii::t('yii', '{attribute} must be repeated exactly.');
w  
Qiang Xue committed
154
				}
w  
Qiang Xue committed
155 156 157
				$condition = 'value!=' . $compareValue;
				break;
			case '!=':
w  
Qiang Xue committed
158
				if ($message === null) {
Alexander Makarov committed
159
					$message = \Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
w  
Qiang Xue committed
160
				}
w  
Qiang Xue committed
161 162 163
				$condition = 'value==' . $compareValue;
				break;
			case '>':
w  
Qiang Xue committed
164
				if ($message === null) {
Alexander Makarov committed
165
					$message = \Yii::t('yii', '{attribute} must be greater than "{compareValue}".');
w  
Qiang Xue committed
166
				}
w  
Qiang Xue committed
167 168 169
				$condition = 'value<=' . $compareValue;
				break;
			case '>=':
w  
Qiang Xue committed
170
				if ($message === null) {
Alexander Makarov committed
171
					$message = \Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".');
w  
Qiang Xue committed
172
				}
w  
Qiang Xue committed
173 174 175
				$condition = 'value<' . $compareValue;
				break;
			case '<':
w  
Qiang Xue committed
176
				if ($message === null) {
Alexander Makarov committed
177
					$message = \Yii::t('yii', '{attribute} must be less than "{compareValue}".');
w  
Qiang Xue committed
178
				}
w  
Qiang Xue committed
179 180 181
				$condition = 'value>=' . $compareValue;
				break;
			case '<=':
w  
Qiang Xue committed
182
				if ($message === null) {
Alexander Makarov committed
183
					$message = \Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".');
w  
Qiang Xue committed
184
				}
w  
Qiang Xue committed
185 186 187
				$condition = 'value>' . $compareValue;
				break;
			default:
w  
Qiang Xue committed
188
				throw new \yii\base\Exception('Invalid operator "' . $this->operator . '".');
w  
Qiang Xue committed
189 190 191 192 193 194 195 196
		}

		$message = strtr($message, array(
			'{attribute}' => $object->getAttributeLabel($attribute),
			'{compareValue}' => $compareTo,
		));

		return "
w  
Qiang Xue committed
197
if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
w  
Qiang Xue committed
198
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
199 200 201 202
}
";
	}
}