CompareValidator.php 8.22 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/
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 12
use Yii;
use yii\base\InvalidConfigException;
w  
Qiang Xue committed
13

w  
Qiang Xue committed
14
/**
w  
Qiang Xue committed
15
 * CompareValidator compares the specified attribute value with another value and validates if they are equal.
w  
Qiang Xue committed
16 17
 *
 * The value being compared with can be another attribute value
w  
Qiang Xue committed
18 19
 * (specified via [[compareAttribute]]) or a constant (specified via
 * [[compareValue]]. When both are specified, the latter takes
w  
Qiang Xue committed
20 21 22 23
 * 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
24
 * The comparison can be either [[strict]] or not.
w  
Qiang Xue committed
25
 *
w  
Qiang Xue committed
26 27
 * CompareValidator supports different comparison operators, specified
 * via the [[operator]] property.
w  
Qiang Xue committed
28 29
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
30
 * @since 2.0
w  
Qiang Xue committed
31
 */
w  
Qiang Xue committed
32
class CompareValidator extends Validator
w  
Qiang Xue committed
33 34
{
	/**
Qiang Xue committed
35 36 37 38 39 40
	 * @var string the name of the attribute to be compared with. When both this property
	 * and [[compareValue]] are set, the latter takes precedence. If neither is set,
	 * it assumes the comparison is against another attribute whose name is formed by
	 * appending '_repeat' to the attribute being validated. For example, if 'password' is
	 * being validated, then the attribute to be compared would be 'password_repeat'.
	 * @see compareValue
w  
Qiang Xue committed
41 42 43
	 */
	public $compareAttribute;
	/**
Qiang Xue committed
44 45 46
	 * @var string the constant value to be compared with. When both this property
	 * and [[compareAttribute]] are set, this property takes precedence.
	 * @see compareAttribute
w  
Qiang Xue committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	 */
	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:
Qiang Xue committed
62 63 64 65 66 67 68 69 70
	 * 
	 * - `=` or `==`: validates to see if the two values are equal. If [[strict]] is true, the comparison
	 *   will be done in strict mode (i.e. checking value type as well).
	 * - `!=`: validates to see if the two values are NOT equal. If [[strict]] is true, the comparison
	 *   will be done in strict mode (i.e. checking value type as well).
	 * - `>`: validates to see if the value being validated is greater than the value being compared with.
	 * - `>=`: validates to see if the value being validated is greater than or equal to the value being compared with.
	 * - `<`: validates to see if the value being validated is less than the value being compared with.
	 * - `<=`: validates to see if the value being validated is less than or equal to the value being compared with.
w  
Qiang Xue committed
71 72 73 74 75 76
	 */
	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
77
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
78
	 * @param string $attribute the attribute being validated
Qiang Xue committed
79
	 * @throws InvalidConfigException if CompareValidator::operator is invalid
w  
Qiang Xue committed
80
	 */
w  
Qiang Xue committed
81
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
82 83
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
84
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
85
			return;
w  
Qiang Xue committed
86 87
		}
		if ($this->compareValue !== null) {
Qiang Xue committed
88
			$compareLabel = $compareValue = $this->compareValue;
Qiang Xue committed
89
		} else {
Qiang Xue committed
90
			$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
w  
Qiang Xue committed
91
			$compareValue = $object->$compareAttribute;
Qiang Xue committed
92
			$compareLabel = $object->getAttributeLabel($compareAttribute);
w  
Qiang Xue committed
93 94
		}

w  
Qiang Xue committed
95
		switch ($this->operator) {
w  
Qiang Xue committed
96 97
			case '=':
			case '==':
w  
Qiang Xue committed
98
				if (($this->strict && $value !== $compareValue) || (!$this->strict && $value != $compareValue)) {
Qiang Xue committed
99 100
					$message = ($this->message !== null) ? $this->message : Yii::t('yii', '{attribute} must be repeated exactly.');
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel));
w  
Qiang Xue committed
101 102 103
				}
				break;
			case '!=':
w  
Qiang Xue committed
104
				if (($this->strict && $value === $compareValue) || (!$this->strict && $value == $compareValue)) {
Qiang Xue committed
105 106
					$message = ($this->message !== null) ? $this->message : Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel, '{compareValue}' => $compareValue));
w  
Qiang Xue committed
107 108 109
				}
				break;
			case '>':
w  
Qiang Xue committed
110
				if ($value <= $compareValue) {
Qiang Xue committed
111 112
					$message = ($this->message !== null) ? $this->message : Yii::t('yii', '{attribute} must be greater than "{compareValue}".');
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel, '{compareValue}' => $compareValue));
w  
Qiang Xue committed
113 114 115
				}
				break;
			case '>=':
w  
Qiang Xue committed
116
				if ($value < $compareValue) {
Qiang Xue committed
117 118
					$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}' => $compareLabel, '{compareValue}' => $compareValue));
w  
Qiang Xue committed
119 120 121
				}
				break;
			case '<':
w  
Qiang Xue committed
122
				if ($value >= $compareValue) {
Qiang Xue committed
123 124
					$message = ($this->message !== null) ? $this->message : Yii::t('yii', '{attribute} must be less than "{compareValue}".');
					$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel, '{compareValue}' => $compareValue));
w  
Qiang Xue committed
125 126 127
				}
				break;
			case '<=':
w  
Qiang Xue committed
128
				if ($value > $compareValue) {
Qiang Xue committed
129 130
					$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}' => $compareLabel, '{compareValue}' => $compareValue));
w  
Qiang Xue committed
131 132 133
				}
				break;
			default:
Qiang Xue committed
134
				throw new InvalidConfigException("Unknown operator: {$this->operator}");
w  
Qiang Xue committed
135 136 137 138 139
		}
	}

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

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

		$message = strtr($message, array(
			'{attribute}' => $object->getAttributeLabel($attribute),
Qiang Xue committed
201
			'{compareValue}' => $compareLabel,
w  
Qiang Xue committed
202 203 204
		));

		return "
w  
Qiang Xue committed
205
if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
w  
Qiang Xue committed
206
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
207 208 209 210
}
";
	}
}