Final Class Yiisoft\Validator\Rule\RegexHandler
| Inheritance | Yiisoft\Validator\Rule\RegexHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
Validates that the value matches the pattern specified in constructor.
See also Yiisoft\Validator\Rule\Regex.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Yiisoft\Validator\Rule\RegexHandler |
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 Regex) {
throw new UnexpectedRuleException(Regex::class, $rule);
}
$result = new Result();
if (!is_string($value)) {
return $result->addError($rule->getIncorrectInputMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'type' => get_debug_type($value),
]);
}
if (
(!$rule->isNot() && !preg_match($rule->getPattern(), $value))
|| ($rule->isNot() && preg_match($rule->getPattern(), $value))
) {
$result->addError($rule->getMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'value' => $value,
]);
}
return $result;
}
Signup or Login in order to comment.