8.5.x file.api.php hook_file_validate(Drupal\file\FileInterface $file)
8.0.x file.api.php hook_file_validate(Drupal\file\FileInterface $file)
8.1.x file.api.php hook_file_validate(Drupal\file\FileInterface $file)
8.2.x file.api.php hook_file_validate(Drupal\file\FileInterface $file)
8.3.x file.api.php hook_file_validate(Drupal\file\FileInterface $file)
8.4.x file.api.php hook_file_validate(Drupal\file\FileInterface $file)
8.6.x file.api.php hook_file_validate(Drupal\file\FileInterface $file)
7.x system.api.php hook_file_validate($file)

Check that files meet a given criteria.

This hook lets modules perform additional validation on files. They're able to report a failure by returning one or more error messages.

Parameters

$file: The file object being validated.

Return value

An array of error messages. If there are no problems with the file return an empty array.

See also

file_validate()

Related topics

2 functions implement hook_file_validate()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

file_managed_file_validate in modules/file/file.module
An #element_validate callback for the managed_file element.
file_test_file_validate in modules/simpletest/tests/file_test.module
Implements hook_file_validate().
1 invocation of hook_file_validate()
file_validate in includes/file.inc
Checks that a file meets the criteria specified by the validators.

File

modules/system/system.api.php, line 2862
Hooks provided by Drupal core and the System module.

Code

function hook_file_validate($file) {
  $errors = array();
  if (empty($file->filename)) {
    $errors[] = t("The file's name is empty. Please give a name to the file.");
  }
  if (strlen($file->filename) > 255) {
    $errors[] = t("The file's name exceeds the 255 characters limit. Please rename the file and try again.");
  }
  return $errors;
}

Comments

adrianlanzi’s picture

How could I check the Form ID of the file being validated?

yurtboy’s picture

You ever figure this one out?

gaellafond’s picture

As someone suggested on Stackoverflow:
http://stackoverflow.com/questions/15395182/how-could-i-check-the-form-i...
you could use the "hook_FORMID_form_alter", but that doesn't prevent the file upload. That validation will upload the file, set the field value then allow you to check if the file is valid.

One other solution is to use the "$file['source']" variable (from the input variable) to ensure you are validating the right field. Obviously, that will work better IF you are not re-using your file field in another form. This solution allow you to validate the file BEFORE it is associated to the file field.

function MYMODULE_file_validate($file) {
	if ($file['source'] === 'field_MYFILEFIELD_und_0') {
		$errors = array();
		$errors[] = 'I dont like that file.';
		return $errors;
	}
}
gaellafond’s picture

I could not find out how to edit my previous post...

$file is an object, therefore, the condition should be:

if ($file->source === 'field_MYFILEFIELD_und_0')
gaellafond’s picture

I've found a solution for your problem. I know, you submitted it 2 years ago, but hopefully that can still be useful for someone.

// hook_form_alter() OR hook_form_FORM_ID_alter()
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  $form['field_MYFILE'][LANGUAGE_NONE][0]['#upload_validators']['MYFILE_VALIDATE'] = array($form_id);
}

// Same as hook_file_validate(), but with the "form_id"
function MYFILE_VALIDATE($file, $form_id) {
  $errors = array();
  $errors[] = "$form_id dont like that file.";
  return $errors;
}

Replace:
MYMODULE with the name of your custom module.
field_MYFILE with the name of the file field
MYFILE_VALIDATE with whatever you like, as long as the function name doesn't clash with something that already exists.

NOTE: The $form and $form_state can also be passed in parameter, but it's a bad idea. It will be serialised and detached from it's context. The values of the $form_state won't represent the current state of the form, but the form's state at the creation of the form. If you need a file validator that depend on the value of other fields, the only solution is to use "hook_form_alter()" and set a "$form['validate'][] = 'MYVALIDATOR';".

arne_hortell’s picture

Even though its a while back, it helped me to understand why things got magic...
Any idea to validate with the upload_validators even though its necessary to get current form settings?

p0pemar1naru’s picture

The following code should always print the error on every file upload
function MODULENAME_file_validate($file) {
$errors = array();
$errors[] = 'This error should always print.';
return $errors;
}
but, it fails if the upload happens trough /admin/content/file + Add file (or /file/add).
Will work just fine if the upload happens trough /node/nid/edit (Media browser) or /file/fid/edit?destination=admin/content/file (Replace file).
Any idea why?

1kubik’s picture

for me its working!

did you cleared the cache?
or try to uninstall and reinstall the module may solves the prob.

drush dis MODULENAME -y;drush en MODULENAME -y;drush cc all;