Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
7.7 KiB

13 years ago
<?php
/**
13 years ago
* CompareValidator class file.
13 years ago
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\validators;
13 years ago
/**
13 years ago
* CompareValidator compares the specified attribute value with another value and validates if they are equal.
13 years ago
*
* The value being compared with can be another attribute value
13 years ago
* (specified via [[compareAttribute]]) or a constant (specified via
* [[compareValue]]. When both are specified, the latter takes
13 years ago
* precedence. If neither is specified, the attribute will be compared
* with another attribute whose name is by appending "_repeat" to the source
* attribute name.
*
13 years ago
* The comparison can be either [[strict]] or not.
13 years ago
*
13 years ago
* CompareValidator supports different comparison operators, specified
* via the [[operator]] property.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class CompareValidator extends Validator
13 years ago
{
/**
* @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>
13 years ago
* <li>'=' or '==': validates to see if the two values are equal. If [[strict]] is true, the comparison
13 years ago
* will be done in strict mode (i.e. checking value type as well).</li>
13 years ago
* <li>'!=': validates to see if the two values are NOT equal. If [[strict]] is true, the comparison
13 years ago
* 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.
13 years ago
* @param \yii\base\Model $object the object being validated
13 years ago
* @param string $attribute the attribute being validated
13 years ago
* @throws \yii\base\Exception if CompareValidator::operator is invalid
13 years ago
*/
13 years ago
public function validateAttribute($object, $attribute)
13 years ago
{
$value = $object->$attribute;
13 years ago
if ($this->allowEmpty && $this->isEmpty($value)) {
13 years ago
return;
13 years ago
}
if ($this->compareValue !== null) {
13 years ago
$compareTo = $compareValue = $this->compareValue;
13 years ago
} else {
13 years ago
$compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute;
13 years ago
$compareValue = $object->$compareAttribute;
$compareTo = $object->getAttributeLabel($compareAttribute);
}
13 years ago
switch ($this->operator) {
13 years ago
case '=':
case '==':
13 years ago
if (($this->strict && $value !== $compareValue) || (!$this->strict && $value != $compareValue)) {
13 years ago
$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be repeated exactly.');
13 years ago
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo));
}
break;
case '!=':
13 years ago
if (($this->strict && $value === $compareValue) || (!$this->strict && $value == $compareValue)) {
13 years ago
$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
13 years ago
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
case '>':
13 years ago
if ($value <= $compareValue) {
13 years ago
$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be greater than "{compareValue}".');
13 years ago
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
case '>=':
13 years ago
if ($value < $compareValue) {
13 years ago
$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".');
13 years ago
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
case '<':
13 years ago
if ($value >= $compareValue) {
13 years ago
$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be less than "{compareValue}".');
13 years ago
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
case '<=':
13 years ago
if ($value > $compareValue) {
13 years ago
$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".');
13 years ago
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
default:
13 years ago
throw new \yii\base\Exception('Invalid operator "' . $this->operator . '".');
13 years ago
}
}
/**
* Returns the JavaScript needed for performing client-side validation.
13 years ago
* @param \yii\base\Model $object the data object being validated
13 years ago
* @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
13 years ago
*/
public function clientValidateAttribute($object, $attribute)
{
13 years ago
if ($this->compareValue !== null) {
13 years ago
$compareTo = $this->compareValue;
13 years ago
$compareValue = json_encode($this->compareValue);
13 years ago
} else {
13 years ago
$compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute;
13 years ago
$compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()";
$compareTo = $object->getAttributeLabel($compareAttribute);
}
$message = $this->message;
13 years ago
switch ($this->operator) {
13 years ago
case '=':
case '==':
13 years ago
if ($message === null) {
13 years ago
$message = \Yii::t('yii', '{attribute} must be repeated exactly.');
13 years ago
}
13 years ago
$condition = 'value!=' . $compareValue;
break;
case '!=':
13 years ago
if ($message === null) {
13 years ago
$message = \Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
13 years ago
}
13 years ago
$condition = 'value==' . $compareValue;
break;
case '>':
13 years ago
if ($message === null) {
13 years ago
$message = \Yii::t('yii', '{attribute} must be greater than "{compareValue}".');
13 years ago
}
13 years ago
$condition = 'value<=' . $compareValue;
break;
case '>=':
13 years ago
if ($message === null) {
13 years ago
$message = \Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".');
13 years ago
}
13 years ago
$condition = 'value<' . $compareValue;
break;
case '<':
13 years ago
if ($message === null) {
13 years ago
$message = \Yii::t('yii', '{attribute} must be less than "{compareValue}".');
13 years ago
}
13 years ago
$condition = 'value>=' . $compareValue;
break;
case '<=':
13 years ago
if ($message === null) {
13 years ago
$message = \Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".');
13 years ago
}
13 years ago
$condition = 'value>' . $compareValue;
break;
default:
13 years ago
throw new \yii\base\Exception('Invalid operator "' . $this->operator . '".');
13 years ago
}
$message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{compareValue}' => $compareTo,
));
return "
13 years ago
if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
13 years ago
messages.push(" . json_encode($message) . ");
13 years ago
}
";
}
}