Final Class Yiisoft\Validator\Rule\CallbackHandler
| Inheritance | Yiisoft\Validator\Rule\CallbackHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
Validates a value using a callback.
See also Yiisoft\Validator\Rule\Callback.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Yiisoft\Validator\Rule\CallbackHandler |
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 | |
| throws | Yiisoft\Validator\Exception\InvalidCallbackReturnTypeException | |
|---|---|---|
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof Callback) {
throw new UnexpectedRuleException(Callback::class, $rule);
}
$callback = $rule->getCallback();
if ($callback === null) {
throw new InvalidArgumentException('Using method outside of attribute scope is prohibited.');
}
$result = $callback($value, $rule, $context);
if (!$result instanceof Result) {
throw new InvalidCallbackReturnTypeException($result);
}
return $result;
}
Signup or Login in order to comment.