Yii2 Bootstrap 3
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.

232 lines
9.1 KiB

14 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
14 years ago
namespace yii\validators;
use Yii;
use yii\base\InvalidConfigException;
14 years ago
14 years ago
/**
14 years ago
* CompareValidator compares the specified attribute value with another value and validates if they are equal.
14 years ago
*
* The value being compared with can be another attribute value
14 years ago
* (specified via [[compareAttribute]]) or a constant (specified via
* [[compareValue]]. When both are specified, the latter takes
14 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.
*
14 years ago
* The comparison can be either [[strict]] or not.
14 years ago
*
14 years ago
* CompareValidator supports different comparison operators, specified
* via the [[operator]] property.
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
14 years ago
* @since 2.0
14 years ago
*/
14 years ago
class CompareValidator extends Validator
14 years ago
{
/**
* @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
14 years ago
*/
public $compareAttribute;
/**
* @var string the constant value to be compared with. When both this property
* and [[compareAttribute]] are set, this property takes precedence.
* @see compareAttribute
14 years ago
*/
public $compareValue;
/**
* @var string the operator for comparison. The following operators are supported:
*
* - '==': validates to see if the two values are equal. The comparison is done is non-strict mode.
* - '===': validates to see if the two values are equal. The comparison is done is strict mode.
* - '!=': validates to see if the two values are NOT equal. The comparison is done is non-strict mode.
* - '!==': validates to see if the two values are NOT equal. The comparison is done is strict mode.
* - `>`: 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.
14 years ago
*/
public $operator = '=';
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
14 years ago
* @param \yii\base\Model $object the object being validated
14 years ago
* @param string $attribute the attribute being validated
* @throws InvalidConfigException if CompareValidator::operator is invalid
14 years ago
*/
14 years ago
public function validateAttribute($object, $attribute)
14 years ago
{
$value = $object->$attribute;
14 years ago
if ($this->compareValue !== null) {
$compareLabel = $compareValue = $this->compareValue;
13 years ago
} else {
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
14 years ago
$compareValue = $object->$compareAttribute;
$compareLabel = $object->getAttributeLabel($compareAttribute);
14 years ago
}
14 years ago
switch ($this->operator) {
14 years ago
case '==':
if ($value != $compareValue) {
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be repeated exactly.');
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel));
}
break;
case '===':
if ($value !== $compareValue) {
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be repeated exactly.');
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel));
14 years ago
}
break;
case '!=':
if ($value == $compareValue) {
$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));
}
break;
case '!==':
if ($value === $compareValue) {
$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));
14 years ago
}
break;
case '>':
14 years ago
if ($value <= $compareValue) {
$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));
14 years ago
}
break;
case '>=':
14 years ago
if ($value < $compareValue) {
$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));
14 years ago
}
break;
case '<':
14 years ago
if ($value >= $compareValue) {
$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));
14 years ago
}
break;
case '<=':
14 years ago
if ($value > $compareValue) {
$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));
14 years ago
}
break;
default:
throw new InvalidConfigException("Unknown operator: {$this->operator}");
14 years ago
}
}
/**
* Validates the given value.
* @param mixed $value the value to be validated.
* @return boolean whether the value is valid.
*/
public function validateValue($value)
{
if ($this->compareValue === null) {
throw new InvalidConfigException('CompareValidator::compareValue must be set.');
}
switch ($this->operator) {
case '==': return $value == $this->compareValue;
case '===': return $value === $this->compareValue;
case '!=': return $value != $this->compareValue;
case '!==': return $value !== $this->compareValue;
case '>': return $value > $this->compareValue;
case '>=': return $value >= $this->compareValue;
case '<': return $value < $this->compareValue;
case '<=': return $value <= $this->compareValue;
default:
throw new InvalidConfigException("Unknown operator \"{$this->operator}\"");
}
}
/**
14 years ago
* Returns the JavaScript needed for performing client-side validation.
14 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 InvalidConfigException if CompareValidator::operator is invalid
14 years ago
*/
public function clientValidateAttribute($object, $attribute)
{
14 years ago
if ($this->compareValue !== null) {
$compareLabel = $this->compareValue;
14 years ago
$compareValue = json_encode($this->compareValue);
13 years ago
} else {
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
14 years ago
$compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()";
$compareLabel = $object->getAttributeLabel($compareAttribute);
14 years ago
}
$message = $this->message;
14 years ago
switch ($this->operator) {
14 years ago
case '=':
case '==':
14 years ago
if ($message === null) {
$message = Yii::t('yii|{attribute} must be repeated exactly.');
14 years ago
}
14 years ago
$condition = 'value!=' . $compareValue;
break;
case '!=':
14 years ago
if ($message === null) {
$message = Yii::t('yii|{attribute} must not be equal to "{compareValue}".');
14 years ago
}
14 years ago
$condition = 'value==' . $compareValue;
break;
case '>':
14 years ago
if ($message === null) {
$message = Yii::t('yii|{attribute} must be greater than "{compareValue}".');
14 years ago
}
14 years ago
$condition = 'value<=' . $compareValue;
break;
case '>=':
14 years ago
if ($message === null) {
$message = Yii::t('yii|{attribute} must be greater than or equal to "{compareValue}".');
14 years ago
}
14 years ago
$condition = 'value<' . $compareValue;
break;
case '<':
14 years ago
if ($message === null) {
$message = Yii::t('yii|{attribute} must be less than "{compareValue}".');
14 years ago
}
14 years ago
$condition = 'value>=' . $compareValue;
break;
case '<=':
14 years ago
if ($message === null) {
$message = Yii::t('yii|{attribute} must be less than or equal to "{compareValue}".');
14 years ago
}
14 years ago
$condition = 'value>' . $compareValue;
break;
default:
throw new InvalidConfigException("Unknown operator: {$this->operator}");
14 years ago
}
$message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{compareValue}' => $compareLabel,
14 years ago
));
return "
14 years ago
if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
14 years ago
messages.push(" . json_encode($message) . ");
14 years ago
}
";
}
}