Same name and namespace in other branches
  1. 7.x includes/file.inc \file_validate()
  2. 8.9.x core/modules/file/file.module \file_validate()
  3. 9 core/modules/file/file.module \file_validate()

Checks that a file meets the criteria specified by the validators.

After executing the validator callbacks specified hook_file_validate() will also be called to allow other modules to report errors about the file.

Parameters

\Drupal\file\FileInterface $file: A file entity.

array $validators: (optional) An associative array of callback functions used to validate the file. The keys are function names and the values arrays of callback parameters which will be passed in after the file entity. The functions should return an array of error messages; an empty array indicates that the file passed validation. The callback functions will be called in the order specified in the array, then the hook hook_file_validate() will be invoked so other modules can validate the new file.

Return value

array An array containing validation error messages.

Deprecated

in drupal:10.2.0 and is removed from drupal:11.0.0. Use the 'file.validator' service instead.

See also

https://www.drupal.org/node/3363700

2 calls to file_validate()
LegacyValidateTest::testCallerValidation in core/modules/file/tests/src/Kernel/LegacyValidateTest.php
Tests that the validators passed into are checked.
LegacyValidateTest::testInsecureExtensions in core/modules/file/tests/src/Kernel/LegacyValidateTest.php
Tests hard-coded security check in file_validate().

File

core/modules/file/file.module, line 96
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function file_validate(FileInterface $file, $validators = []) {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'file.validator\' service instead. See https://www.drupal.org/node/3363700', E_USER_DEPRECATED);
  $violations = \Drupal::service('file.validator')
    ->validate($file, $validators);
  $errors = [];
  foreach ($violations as $violation) {
    $errors[] = $violation
      ->getMessage();
  }
  return $errors;
}