W3cubDocs

/Drupal 8

function file_validate

file_validate(FileInterface $file, $validators = array())

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.

See also

hook_file_validate()

File

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

Code

function file_validate(FileInterface $file, $validators = array()) {
  // Call the validation functions specified by this function's caller.
  $errors = array();
  foreach ($validators as $function => $args) {
    if (function_exists($function)) {
      array_unshift($args, $file);
      $errors = array_merge($errors, call_user_func_array($function, $args));
    }
  }

  // Let other modules perform validation on the new file.
  return array_merge($errors, \Drupal::moduleHandler()->invokeAll('file_validate', array($file)));
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!file!file.module/function/file_validate/8.1.x