Final Class Yiisoft\Validator\Rule\LengthHandler
| Inheritance | Yiisoft\Validator\Rule\LengthHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
| Uses Traits | Yiisoft\Validator\Rule\Trait\CountableLimitHandlerTrait |
Validates that the value is a string of a certain length.
See also Yiisoft\Validator\Rule\Length.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Yiisoft\Validator\Rule\LengthHandler |
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($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;
}
Signup or Login in order to comment.