Posted by thesubtledoctor on July 16, 2009 at 8:38am
Jump to:
| Project: | Register with Picture |
| Version: | 6.x-1.0 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | mmilano |
| Status: | closed (fixed) |
Issue Summary
The form API says that the #required property is not allowed for file elements - there's a patch out there that allows the property and places an asterisk next to the label as desired when it is set to 'TRUE', but it still doesn't actually validate for the presence of an uploaded file. I'd like to write a custom validation function, but the file validation functions all seem to apply only in the case where a file has already been uploaded. Can anybody help me write a custom validation function to make a file upload field required?? This is my attempt that doesn't work:
function mymodule_user_register_validate($form_id, $form_values, &$form){
if (count($_FILES)==0) {
form_set_error('picture_upload_register', t('PROB'));
}
}
Comments
#1
I tried an idiot test - adding a function to the validators array of the file_save_upload function that always returns an error message - see below. It didn't work. Any ideas why this could be?
/**
* Implementation of hook_user()
*/
function reg_with_pic_user($op, &$edit, &$user, $category=null) {
if (variable_get('user_pictures', 0)) {
switch ($op) {
case 'register':
// setup picture upload in registration form
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1, '#required' => 'TRUE');
$form['picture']['picture_upload_register'] = array('#type' => 'file', '#title' => t('Upload picture'), '#description' => t('<br>Your virtual face or picture. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', ''));
return $form;
break;
case 'validate':
// validate uploaded picture, taken from user module
$validators = 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_uploaded' => array(),
);
$file = file_save_upload('picture_upload_register', $validators);
break;
case 'insert':
// file repopulates from uploadcache
$file = file_save_upload('picture_upload_register');
if ($file) {
$info = image_get_info($file->filepath);
// save picture to correct path and update the row in the user table
$destination = variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid .'.'. $info['extension'];
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
db_query("UPDATE {users} SET picture='%s' WHERE uid=%d", $file->filepath, $user->uid);
}
}
}
}
}
function file_uploaded($file) {
$errors = array();
$errors[] = t('You must upload a file');
return $errors;
}
#2
i just did this, seems to work... i am not a pro
<?php
case 'validate':
// validate uploaded picture, taken from user module
$validators = 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);
if (!$file) {
form_set_error('picture_upload_register', t('You must select a valid file to upload.'));
}
break;
?>
just dont know how to get the red star to the field...
#3
I added an admin settings page where you can select if the field is required or not. Also the form logic to support a required field.
#4
Automatically closed -- issue fixed for 2 weeks with no activity.
#5
http://drupal.org/node/271476