Final Class Yiisoft\Validator\Rule\FilledOnlyOneOfHandler
| Inheritance | Yiisoft\Validator\Rule\FilledOnlyOneOfHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
| Uses Traits | Yiisoft\Validator\Rule\Trait\TranslatedPropertiesHandlerTrait |
Validates that only one of specified properties is filled.
See also Yiisoft\Validator\Rule\FilledOnlyOneOf.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Yiisoft\Validator\Rule\FilledOnlyOneOfHandler |
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 FilledOnlyOneOf) {
throw new UnexpectedRuleException(FilledOnlyOneOf::class, $rule);
}
/** @var mixed $value */
$value = $context->getParameter(ValidationContext::PARAMETER_VALUE_AS_ARRAY) ?? $value;
$result = new Result();
if (!is_array($value) && !is_object($value)) {
return $result->addError($rule->getIncorrectInputMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'type' => get_debug_type($value),
]);
}
$filledCount = 0;
foreach ($rule->getProperties() as $property) {
if (!(new WhenEmpty())(ArrayHelper::getValue($value, $property), $context->isPropertyMissing())) {
$filledCount++;
}
if ($filledCount > 1) {
return $this->getGenericErrorResult($rule, $context);
}
}
return $filledCount === 1 ? $result : $this->getGenericErrorResult($rule, $context);
}
Signup or Login in order to comment.