|
|
@ -59,82 +59,91 @@ class CompareValidator extends Validator |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $operator = '='; |
|
|
|
public $operator = '='; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Validates the attribute of the object. |
|
|
|
* Initializes the 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 InvalidConfigException if CompareValidator::operator is invalid |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function validateAttribute($object, $attribute) |
|
|
|
public function init() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$value = $object->$attribute; |
|
|
|
parent::init(); |
|
|
|
if ($this->compareValue !== null) { |
|
|
|
if ($this->message === null) { |
|
|
|
$compareLabel = $compareValue = $this->compareValue; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; |
|
|
|
|
|
|
|
$compareValue = $object->$compareAttribute; |
|
|
|
|
|
|
|
$compareLabel = $object->getAttributeLabel($compareAttribute); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch ($this->operator) { |
|
|
|
switch ($this->operator) { |
|
|
|
case '==': |
|
|
|
case '==': |
|
|
|
if ($value != $compareValue) { |
|
|
|
$this->message = Yii::t('yii|{attribute} must be repeated exactly.'); |
|
|
|
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be repeated exactly.'); |
|
|
|
|
|
|
|
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case '===': |
|
|
|
case '===': |
|
|
|
if ($value !== $compareValue) { |
|
|
|
$this->message = Yii::t('yii|{attribute} must be repeated exactly.'); |
|
|
|
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be repeated exactly.'); |
|
|
|
|
|
|
|
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case '!=': |
|
|
|
case '!=': |
|
|
|
if ($value == $compareValue) { |
|
|
|
$this->message = Yii::t('yii|{attribute} must not be equal to "{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; |
|
|
|
break; |
|
|
|
case '!==': |
|
|
|
case '!==': |
|
|
|
if ($value === $compareValue) { |
|
|
|
$this->message = Yii::t('yii|{attribute} must not be equal to "{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; |
|
|
|
break; |
|
|
|
case '>': |
|
|
|
case '>': |
|
|
|
if ($value <= $compareValue) { |
|
|
|
$this->message = Yii::t('yii|{attribute} must be greater than "{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; |
|
|
|
break; |
|
|
|
case '>=': |
|
|
|
case '>=': |
|
|
|
if ($value < $compareValue) { |
|
|
|
$this->message = Yii::t('yii|{attribute} must be greater than or equal to "{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; |
|
|
|
break; |
|
|
|
case '<': |
|
|
|
case '<': |
|
|
|
if ($value >= $compareValue) { |
|
|
|
$this->message = Yii::t('yii|{attribute} must be less than "{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; |
|
|
|
break; |
|
|
|
case '<=': |
|
|
|
case '<=': |
|
|
|
if ($value > $compareValue) { |
|
|
|
$this->message = Yii::t('yii|{attribute} must be less than or equal to "{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; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
throw new InvalidConfigException("Unknown operator: {$this->operator}"); |
|
|
|
throw new InvalidConfigException("Unknown operator: {$this->operator}"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Validates the attribute of the object. |
|
|
|
|
|
|
|
* 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 InvalidConfigException if CompareValidator::operator is invalid |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function validateAttribute($object, $attribute) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$value = $object->$attribute; |
|
|
|
|
|
|
|
if (is_array($value)) { |
|
|
|
|
|
|
|
$this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.')); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if ($this->compareValue !== null) { |
|
|
|
|
|
|
|
$compareLabel = $compareValue = $this->compareValue; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; |
|
|
|
|
|
|
|
$compareValue = $object->$compareAttribute; |
|
|
|
|
|
|
|
$compareLabel = $object->getAttributeLabel($compareAttribute); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch ($this->operator) { |
|
|
|
|
|
|
|
case '==': $valid = $value == $compareValue; break; |
|
|
|
|
|
|
|
case '===': $valid = $value === $compareValue; break; |
|
|
|
|
|
|
|
case '!=': $valid = $value != $compareValue; break; |
|
|
|
|
|
|
|
case '!==': $valid = $value !== $compareValue; break; |
|
|
|
|
|
|
|
case '>': $valid = $value > $compareValue; break; |
|
|
|
|
|
|
|
case '>=': $valid = $value >= $compareValue; break; |
|
|
|
|
|
|
|
case '<': $valid = $value < $compareValue; break; |
|
|
|
|
|
|
|
case '<=': $valid = $value <= $compareValue; break; |
|
|
|
|
|
|
|
default: $valid = false; break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!$valid) { |
|
|
|
|
|
|
|
$this->addError($object, $attribute, $this->message, array( |
|
|
|
|
|
|
|
'{compareAttribute}' => $compareLabel, |
|
|
|
|
|
|
|
'{compareValue}' => $compareValue, |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Validates the given value. |
|
|
|
* Validates the given value. |
|
|
|
* @param mixed $value the value to be validated. |
|
|
|
* @param mixed $value the value to be validated. |
|
|
|
* @return boolean whether the value is valid. |
|
|
|
* @return boolean whether the value is valid. |
|
|
|
|
|
|
|
* @throws InvalidConfigException if [[compareValue]] is not set. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function validateValue($value) |
|
|
|
public function validateValue($value) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -151,8 +160,6 @@ class CompareValidator extends Validator |
|
|
|
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}\""); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -173,51 +180,8 @@ class CompareValidator extends Validator |
|
|
|
$compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()"; |
|
|
|
$compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()"; |
|
|
|
$compareLabel = $object->getAttributeLabel($compareAttribute); |
|
|
|
$compareLabel = $object->getAttributeLabel($compareAttribute); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$condition = "value {$this->operator} $compareValue"; |
|
|
|
$message = $this->message; |
|
|
|
$message = strtr($this->message, array( |
|
|
|
switch ($this->operator) { |
|
|
|
|
|
|
|
case '=': |
|
|
|
|
|
|
|
case '==': |
|
|
|
|
|
|
|
if ($message === null) { |
|
|
|
|
|
|
|
$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}".'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$condition = 'value==' . $compareValue; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case '>': |
|
|
|
|
|
|
|
if ($message === null) { |
|
|
|
|
|
|
|
$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}".'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$condition = 'value<' . $compareValue; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case '<': |
|
|
|
|
|
|
|
if ($message === null) { |
|
|
|
|
|
|
|
$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}".'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$condition = 'value>' . $compareValue; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
throw new InvalidConfigException("Unknown operator: {$this->operator}"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$message = strtr($message, array( |
|
|
|
|
|
|
|
'{attribute}' => $object->getAttributeLabel($attribute), |
|
|
|
'{attribute}' => $object->getAttributeLabel($attribute), |
|
|
|
'{compareValue}' => $compareLabel, |
|
|
|
'{compareValue}' => $compareLabel, |
|
|
|
)); |
|
|
|
)); |
|
|
|