You are here

protected function FileUploadResource::validate in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/file/src/Plugin/rest/resource/FileUploadResource.php \Drupal\file\Plugin\rest\resource\FileUploadResource::validate()
  2. 9 core/modules/file/src/Plugin/rest/resource/FileUploadResource.php \Drupal\file\Plugin\rest\resource\FileUploadResource::validate()

Validates the file.

@todo this method is unused in this class because file validation needs to be split up in 2 steps in ::post(). Add a deprecation notice as soon as a central core file upload service can be used in this class. See https://www.drupal.org/project/drupal/issues/2940383

Parameters

\Drupal\file\FileInterface $file: The file entity to validate.

array $validators: An array of upload validators to pass to file_validate().

Throws

\Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException Thrown when there are file validation errors.

File

core/modules/file/src/Plugin/rest/resource/FileUploadResource.php, line 470

Class

FileUploadResource
File upload resource.

Namespace

Drupal\file\Plugin\rest\resource

Code

protected function validate(FileInterface $file, array $validators) {
  $this
    ->resourceValidate($file);

  // Validate the file based on the field definition configuration.
  $errors = file_validate($file, $validators);
  if (!empty($errors)) {
    $message = "Unprocessable Entity: file validation failed.\n";
    $message .= implode("\n", array_map(function ($error) {
      return PlainTextOutput::renderFromHtml($error);
    }, $errors));
    throw new UnprocessableEntityHttpException($message);
  }
}