Final Class Yiisoft\Validator\Rule\FileHandler
| Inheritance | Yiisoft\Validator\Rule\FileHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
Validates that a value is a file and optionally checks its extension, MIME type and size.
See also Yiisoft\Validator\Rule\File.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Yiisoft\Validator\Rule\FileHandler |
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 File) {
throw new UnexpectedRuleException(File::class, $rule);
}
$result = new Result();
$file = $this->getFileData($value);
if ($file['status'] === 'missing') {
$result->addError($rule->getUploadRequiredMessage(), $this->getParameters($context));
return $result;
}
if ($file['status'] === 'upload-error') {
$result->addError(
$rule->getUploadFailedMessage(),
$this->getParameters($context, $file, ['error' => $file['error']]),
);
return $result;
}
if ($file['status'] !== 'ok') {
$result->addError($rule->getMessage(), $this->getParameters($context, $file));
return $result;
}
if (!$this->isExtensionValid($file['name'], $rule->getExtensions())) {
$result->addError(
$rule->getWrongExtensionMessage(),
$this->getParameters($context, $file, ['extensions' => implode(', ', $rule->getExtensions() ?? [])]),
);
}
if (!$this->isMimeTypeValid($file, $rule->getMimeTypes(), $rule->getTrustClientMediaType())) {
$result->addError(
$rule->getWrongMimeTypeMessage(),
$this->getParameters($context, $file, ['mimeTypes' => implode(', ', $rule->getMimeTypes() ?? [])]),
);
}
$this->validateSize($file, $rule, $context, $result);
return $result;
}
Signup or Login in order to comment.