0 follower

Final Class Yiisoft\Validator\Rule\SubsetHandler

InheritanceYiisoft\Validator\Rule\SubsetHandler
ImplementsYiisoft\Validator\RuleHandlerInterface

A handler for {@see Subset} rule. Validates that the set of values is a subset of another set.

Public Methods

Hide inherited methods

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

Method Details

Hide inherited methods

validate() public method

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 Subset) {
        throw new UnexpectedRuleException(Subset::class, $rule);
    }
    if (!is_iterable($value)) {
        return (new Result())->addError($rule->getIncorrectInputMessage(), [
            'property' => $context->getTranslatedProperty(),
            'Property' => $context->getCapitalizedTranslatedProperty(),
            'type' => get_debug_type($value),
        ]);
    }
    if (!ArrayHelper::isSubset($value, $rule->getValues(), $rule->isStrict())) {
        return (new Result())->addError($rule->getMessage(), [
            'property' => $context->getTranslatedProperty(),
            'Property' => $context->getCapitalizedTranslatedProperty(),
        ]);
    }
    return new Result();
}