Final Class Yiisoft\Validator\Rule\CompareHandler
| Inheritance | Yiisoft\Validator\Rule\CompareHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
Compares the specified value with "target" value provided directly or within a property.
See also:
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Yiisoft\Validator\Rule\CompareHandler |
Method Details
| public Yiisoft\Validator\Result validate ( mixed $value, Yiisoft\Validator\RuleInterface $rule, Yiisoft\Validator\ValidationContext $context ) | ||
| $value | mixed | |
| $rule | Yiisoft\Validator\RuleInterface | |
| $context | Yiisoft\Validator\ValidationContext | |
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof AbstractCompare) {
throw new UnexpectedRuleException(AbstractCompare::class, $rule);
}
$result = new Result();
if (!$this->isInputCorrect($rule->getType(), $value)) {
return $result->addError($rule->getIncorrectInputMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'type' => get_debug_type($value),
]);
}
/** @var mixed $targetValue */
$targetValue = $rule->getTargetValue();
$targetProperty = $rule->getTargetProperty();
if ($targetValue === null && $targetProperty !== null) {
/** @var mixed $targetValue */
$targetValue = $context->getDataSet()->getPropertyValue($targetProperty);
if (!$this->isInputCorrect($rule->getType(), $targetValue)) {
return $result->addError($rule->getIncorrectDataSetTypeMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'type' => get_debug_type($targetValue),
]);
}
}
if ($this->compareValues($rule->getType(), $rule->getOperator(), $value, $targetValue)) {
return new Result();
}
$capitalizedTargetProperty = $targetProperty ? StringHelper::uppercaseFirstCharacter($targetProperty) : null;
return (new Result())->addError($rule->getMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'targetValue' => $this->getFormattedValue($rule->getTargetValue()),
'targetProperty' => $targetProperty,
'TargetProperty' => $capitalizedTargetProperty,
'targetPropertyValue' => $targetProperty !== null ? $this->getFormattedValue($targetValue) : null,
'targetValueOrProperty' => $targetProperty ?? $this->getFormattedValue($targetValue),
'TargetValueOrProperty' => $capitalizedTargetProperty ?? $this->getFormattedValue($targetValue),
'value' => $this->getFormattedValue($value),
]);
}
Signup or Login in order to comment.