Is it possible to create custom validators for file_save_upload?
From http://drupal.org/node/286374 I think it's possible. I'm twiking some code to make the profile picture required in registration form and since '#required' doesn't work for upload fields (see: http://drupal.org/node/42762 ), I thought that maybe creating a custom validation would do the trick.
I've started from the code of http://drupal.org/project/reg_with_pic which adds the upload picture field on registration form.
I cannot find documentation on how to implement custom validators. For now I have this code that does not work:
$validators = array(
'reg_with_pic_file_required' => array(),
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);
$file = file_save_upload('picture_upload_register', $validators);
and
function reg_with_pic_file_required(&$file){
$errors = array();
if(!isset($file->filepath)){
$errors[] = t('Image is required. Please upload an image');
}
return $errors;
}
It never calls the reg_with_pic_file_required function. I've tried putting some drupal_set_message's but they never show so I assume that this function is never called. Please note that the checking I do on the "if" is temporal, I'll look into a proper checking when I know the function gets called.
Anyone can tell me what am I doing wrong and how can I solve this?
Comments
I've found why it does not work
Ok, I've seen the implementation of file_save_upload and it first works with the file uploaded and then makes the checkings from validation functions. In fact, if I upload a correct image, it does indeed the custom checking, but if I leave the field empty, it never arrives to the validators checking.
My fault, sorry. I'll try to check if I can stop somehow on 'validate' $op in hook_user when there is no file.