|
|
|
@ -8,6 +8,8 @@
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
namespace yii\validators; |
|
|
|
|
use Yii; |
|
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* CompareValidator compares the specified attribute value with another value and validates if they are equal. |
|
|
|
@ -30,11 +32,18 @@ namespace yii\validators;
|
|
|
|
|
class CompareValidator extends Validator |
|
|
|
|
{ |
|
|
|
|
/** |
|
|
|
|
* @var string the name of the attribute to be compared with |
|
|
|
|
* @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 |
|
|
|
|
*/ |
|
|
|
|
public $compareAttribute; |
|
|
|
|
/** |
|
|
|
|
* @var string the constant value to be compared with |
|
|
|
|
* @var string the constant value to be compared with. When both this property |
|
|
|
|
* and [[compareAttribute]] are set, this property takes precedence. |
|
|
|
|
* @see compareAttribute |
|
|
|
|
*/ |
|
|
|
|
public $compareValue; |
|
|
|
|
/** |
|
|
|
@ -50,16 +59,15 @@ class CompareValidator extends Validator
|
|
|
|
|
/** |
|
|
|
|
* @var string the operator for comparison. Defaults to '='. |
|
|
|
|
* The followings are valid operators: |
|
|
|
|
* <ul> |
|
|
|
|
* <li>'=' 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).</li> |
|
|
|
|
* <li>'!=': 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).</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> |
|
|
|
|
* |
|
|
|
|
* - `=` 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. |
|
|
|
|
*/ |
|
|
|
|
public $operator = '='; |
|
|
|
|
|
|
|
|
@ -68,7 +76,7 @@ class CompareValidator extends Validator
|
|
|
|
|
* If there is any error, the error message is added to the object. |
|
|
|
|
* @param \yii\base\Model $object the object being validated |
|
|
|
|
* @param string $attribute the attribute being validated |
|
|
|
|
* @throws \yii\base\Exception if CompareValidator::operator is invalid |
|
|
|
|
* @throws InvalidConfigException if CompareValidator::operator is invalid |
|
|
|
|
*/ |
|
|
|
|
public function validateAttribute($object, $attribute) |
|
|
|
|
{ |
|
|
|
@ -77,53 +85,53 @@ class CompareValidator extends Validator
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if ($this->compareValue !== null) { |
|
|
|
|
$compareTo = $compareValue = $this->compareValue; |
|
|
|
|
$compareLabel = $compareValue = $this->compareValue; |
|
|
|
|
} else { |
|
|
|
|
$compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute; |
|
|
|
|
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; |
|
|
|
|
$compareValue = $object->$compareAttribute; |
|
|
|
|
$compareTo = $object->getAttributeLabel($compareAttribute); |
|
|
|
|
$compareLabel = $object->getAttributeLabel($compareAttribute); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch ($this->operator) { |
|
|
|
|
case '=': |
|
|
|
|
case '==': |
|
|
|
|
if (($this->strict && $value !== $compareValue) || (!$this->strict && $value != $compareValue)) { |
|
|
|
|
$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be repeated exactly.'); |
|
|
|
|
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo)); |
|
|
|
|
$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 (($this->strict && $value === $compareValue) || (!$this->strict && $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}' => $compareTo, '{compareValue}' => $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 be greater than "{compareValue}".'); |
|
|
|
|
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $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)); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case '>=': |
|
|
|
|
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}' => $compareTo, '{compareValue}' => $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)); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case '<': |
|
|
|
|
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}' => $compareTo, '{compareValue}' => $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)); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case '<=': |
|
|
|
|
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}' => $compareTo, '{compareValue}' => $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)); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
throw new \yii\base\Exception('Invalid operator "' . $this->operator . '".'); |
|
|
|
|
throw new InvalidConfigException("Unknown operator: {$this->operator}"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -132,17 +140,17 @@ class CompareValidator extends Validator
|
|
|
|
|
* @param \yii\base\Model $object the data object being validated |
|
|
|
|
* @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 |
|
|
|
|
* @throws InvalidConfigException if CompareValidator::operator is invalid |
|
|
|
|
*/ |
|
|
|
|
public function clientValidateAttribute($object, $attribute) |
|
|
|
|
{ |
|
|
|
|
if ($this->compareValue !== null) { |
|
|
|
|
$compareTo = $this->compareValue; |
|
|
|
|
$compareLabel = $this->compareValue; |
|
|
|
|
$compareValue = json_encode($this->compareValue); |
|
|
|
|
} else { |
|
|
|
|
$compareAttribute = ($this->compareAttribute === null) ? $attribute . '_repeat' : $this->compareAttribute; |
|
|
|
|
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; |
|
|
|
|
$compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()"; |
|
|
|
|
$compareTo = $object->getAttributeLabel($compareAttribute); |
|
|
|
|
$compareLabel = $object->getAttributeLabel($compareAttribute); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$message = $this->message; |
|
|
|
@ -150,47 +158,47 @@ class CompareValidator extends Validator
|
|
|
|
|
case '=': |
|
|
|
|
case '==': |
|
|
|
|
if ($message === null) { |
|
|
|
|
$message = \Yii::t('yii', '{attribute} must be repeated exactly.'); |
|
|
|
|
$message = Yii::t('yii', '{attribute} must be repeated exactly.'); |
|
|
|
|
} |
|
|
|
|
$condition = 'value!=' . $compareValue; |
|
|
|
|
break; |
|
|
|
|
case '!=': |
|
|
|
|
if ($message === null) { |
|
|
|
|
$message = \Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); |
|
|
|
|
$message = Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); |
|
|
|
|
} |
|
|
|
|
$condition = 'value==' . $compareValue; |
|
|
|
|
break; |
|
|
|
|
case '>': |
|
|
|
|
if ($message === null) { |
|
|
|
|
$message = \Yii::t('yii', '{attribute} must be greater than "{compareValue}".'); |
|
|
|
|
$message = Yii::t('yii', '{attribute} must be greater than "{compareValue}".'); |
|
|
|
|
} |
|
|
|
|
$condition = 'value<=' . $compareValue; |
|
|
|
|
break; |
|
|
|
|
case '>=': |
|
|
|
|
if ($message === null) { |
|
|
|
|
$message = \Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".'); |
|
|
|
|
$message = Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".'); |
|
|
|
|
} |
|
|
|
|
$condition = 'value<' . $compareValue; |
|
|
|
|
break; |
|
|
|
|
case '<': |
|
|
|
|
if ($message === null) { |
|
|
|
|
$message = \Yii::t('yii', '{attribute} must be less than "{compareValue}".'); |
|
|
|
|
$message = Yii::t('yii', '{attribute} must be less than "{compareValue}".'); |
|
|
|
|
} |
|
|
|
|
$condition = 'value>=' . $compareValue; |
|
|
|
|
break; |
|
|
|
|
case '<=': |
|
|
|
|
if ($message === null) { |
|
|
|
|
$message = \Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".'); |
|
|
|
|
$message = Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".'); |
|
|
|
|
} |
|
|
|
|
$condition = 'value>' . $compareValue; |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
throw new \yii\base\Exception('Invalid operator "' . $this->operator . '".'); |
|
|
|
|
throw new InvalidConfigException("Unknown operator: {$this->operator}"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$message = strtr($message, array( |
|
|
|
|
'{attribute}' => $object->getAttributeLabel($attribute), |
|
|
|
|
'{compareValue}' => $compareTo, |
|
|
|
|
'{compareValue}' => $compareLabel, |
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|
return " |
|
|
|
|