0 follower

Final Class Yiisoft\Validator\Rule\LengthHandler

InheritanceYiisoft\Validator\Rule\LengthHandler
ImplementsYiisoft\Validator\RuleHandlerInterface
Uses TraitsYiisoft\Validator\Rule\Trait\CountableLimitHandlerTrait

Validates that the value is a string of a certain length.

See also Yiisoft\Validator\Rule\Length.

Public Methods

Hide inherited methods

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

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($value, RuleInterface $rule, ValidationContext $context): Result
{
    if (!$rule instanceof Length) {
        throw new UnexpectedRuleException(Length::class, $rule);
    }
    $result = new Result();
    if (!is_string($value)) {
        $result->addError($rule->getIncorrectInputMessage(), [
            'property' => $context->getTranslatedProperty(),
            'Property' => $context->getCapitalizedTranslatedProperty(),
            'type' => get_debug_type($value),
        ]);
        return $result;
    }
    $length = mb_strlen($value, $rule->getEncoding());
    $this->validateCountableLimits($rule, $context, $length, $result);
    return $result;
}