I'm not totally sure if this is incorrect documentation or not, so I won't CVS it (and the fact that I'd rather bash myself in the temple with a brick than use CVS has nothing to do with it, really); it may just be that I'm not understanding the docs. But if that's the case, I would argue that it's overly-vague.

In the parameters for file_save_upload():

$validators An optional, 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 user and file objects. The functions should return an array of error messages, an empty array indicates that the file passed validation. The functions will be called in the order specified.

This seems to imply to me that I can do something like this:


function my_module_valudate($node, &$form) {
	file_save_upload('uploaded_file', array('_my_module_file_validator' => array()), file_directory_path(), FILE_EXISTS_REPLACE);
}

function _my_module_file_validator($user, $file) {
	$errors = array();
	// stuff
	return $errors;
}

However, in practice, it seems that the first and only parameter passed to my validator function is the file object. So the user object seemingly is not being passed to the validator. Not a huge tragedy since I can global it if I need to, but it seems the docs is incorrect… Or am I?

CommentFileSizeAuthor
#6 286374-6.x.diff.txt922 bytesjhodgdon
#5 286374.diff1.39 KBjhodgdon

Comments

Garrett Albright’s picture

Investigating this further, I'm noticing parts of core which use these validation functions are not expecting to receive the user object as one of their parameters. See upload_node_form_submit() which lists, among others, file_validate_image_resolution() as a validator. The latter function's parameters are $file, $maximum_dimensions and $minimum_dimensions -- no $user.

Also, in my code above, that should be my_module_validate(), of course. Oh my God.

Noyz’s picture

Component: Developer Guide » Correction/Clarification

Changed the component to reflect the new component categorization. See http://drupal.org/node/301443

johnnoc’s picture

Project: Documentation » Drupal core
Version: » 6.x-dev
Component: Correction/Clarification » documentation

this is regarding api.drupal.org
Moving to Drupal project, Documentation component

jhodgdon’s picture

Assigned: Unassigned » jhodgdon

This is indeed a doc issue in 6.x as well as 7.x. The validation functions only pass the file, not the user object.

jhodgdon’s picture

StatusFileSize
new1.39 KB

Here is a patch for Drupal 7.x

jhodgdon’s picture

StatusFileSize
new922 bytes

Here is a patch for 6.x -- pretty much the same.

jhodgdon’s picture

Assigned: jhodgdon » Unassigned
Status: Active » Needs review
quicksketch’s picture

Status: Needs review » Reviewed & tested by the community

jhodgdon is definitely correct. Only the $file object is passed into validator functions (FileField has four separate validator functions that all work like this), plus the core validator file_validate_image_resolution() works like this also.

quicksketch’s picture

Version: 6.x-dev » 7.x-dev
Priority: Normal » Minor

But lets fix in 7.x first, then we'll move to 6.x

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to DRUPAL-6 and CVS HEAD. Thanks!

drewish’s picture

Version: 7.x-dev » 6.x-dev
Status: Fixed » Patch (to be ported)

Just wanted to weigh in on this. In an early version of the patch that changes this I think we were passing the $user object in. Looks like the docs never got updated. Need to fix this in D6 too.

jhodgdon’s picture

OK, now I am confused.

The current 6.x doc for the function on http://api.drupal.org/api/function/file_save_upload/6 says:

"$validators An optional, 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 object. The functions should return an array of error messages; an empty array indicates that the file passed validation. The functions will be called in the order specified."

It doesn't mention the $user object any more -- it's been patched (there was a patch above, and it got committed).

In 7.x http://api.drupal.org/api/function/file_save_upload/7 it says:

"$validators An optional, associative array of callback functions used to validate the file. See file_validate() for a full discussion of the array format."

That also doesn't mention the $user object.

So I think this is fixed in both D6 and D7 now. So I am not sure why Drewish reopened the ticket... please explain in more detail what you meant still needs to be fixed, and I will be glad to create another patch if necessary, but it looks right to me.

drewish’s picture

Status: Patch (to be ported) » Fixed

because i clearly can't read sorry for the confusion.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.