CompareValidator.php 7.57 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 71
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
72
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
73 74
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
75
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
76
			return;
w  
Qiang Xue committed
77 78
		}
		if ($this->compareValue !== null) {
w  
Qiang Xue committed
79
			$compareTo = $compareValue = $this->compareValue;
w  
Qiang Xue committed
80 81
		}
		else {
w  
Qiang Xue committed
82 83 84 85 86
			$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
			$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)) {
w  
Qiang Xue committed
91 92 93 94 95
					$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be repeated exactly.');
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo));
				}
				break;
			case '!=':
w  
Qiang Xue committed
96
				if (($this->strict && $value === $compareValue) || (!$this->strict && $value == $compareValue)) {
w  
Qiang Xue committed
97 98 99 100 101
					$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
				}
				break;
			case '>':
w  
Qiang Xue committed
102
				if ($value <= $compareValue) {
w  
Qiang Xue committed
103 104 105 106 107
					$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be greater than "{compareValue}".');
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
				}
				break;
			case '>=':
w  
Qiang Xue committed
108
				if ($value < $compareValue) {
w  
Qiang Xue committed
109 110 111 112 113
					$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".');
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
				}
				break;
			case '<':
w  
Qiang Xue committed
114
				if ($value >= $compareValue) {
w  
Qiang Xue committed
115 116 117 118 119
					$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be less than "{compareValue}".');
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
				}
				break;
			case '<=':
w  
Qiang Xue committed
120
				if ($value > $compareValue) {
w  
Qiang Xue committed
121 122 123 124 125
					$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".');
					$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
w  
Qiang Xue committed
133 134 135 136 137
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
	 */
	public function clientValidateAttribute($object, $attribute)
	{
w  
Qiang Xue committed
138
		if ($this->compareValue !== null) {
w  
Qiang Xue committed
139
			$compareTo = $this->compareValue;
w  
Qiang Xue committed
140
			$compareValue = json_encode($this->compareValue);
w  
Qiang Xue committed
141
		}
w  
Qiang Xue committed
142
		else {
w  
Qiang Xue committed
143 144 145 146 147 148
			$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
			$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) {
w  
Qiang Xue 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) {
w  
Qiang Xue 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) {
w  
Qiang Xue 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) {
w  
Qiang Xue 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) {
w  
Qiang Xue 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) {
w  
Qiang Xue 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
}
";
	}
}