0 follower

Final Class Yiisoft\Validator\Rule\InEnumHandler

InheritanceYiisoft\Validator\Rule\InEnumHandler
ImplementsYiisoft\Validator\RuleHandlerInterface

Validates that the value is one of the values of a specified enum.

See also Yiisoft\Validator\Rule\InEnum.

Public Methods

Hide inherited methods

Method Description Defined By
validate() Yiisoft\Validator\Rule\InEnumHandler

Method Details

Hide inherited methods

validate() public method

public Yiisoft\Validator\Result validate ( mixed $value, object $rule, Yiisoft\Validator\ValidationContext $context )
$value mixed
$rule object
$context Yiisoft\Validator\ValidationContext

                public function validate(mixed $value, object $rule, ValidationContext $context): Result
{
    if (!$rule instanceof InEnum) {
        throw new UnexpectedRuleException(InEnum::class, $rule);
    }
    $result = new Result();
    if ($rule->isNot() === in_array($value, $rule->getValues(), $rule->isStrict())) {
        $result->addError(
            $rule->getMessage(),
            [
                'property' => $context->getTranslatedProperty(),
                'Property' => $context->getCapitalizedTranslatedProperty(),
            ],
        );
    }
    return $result;
}