I have the following code:

if ($image_file = file_save_upload('radio_image', $validators, file_directory_path())) { 
			$result = db_query("SELECT oid FROM {uc_radio_images} WHERE oid = %d AND aid = %d", $option_id, $attribute_id);
			if (db_result($result)) {
				db_query("UPDATE {uc_radio_images} SET image_path = '%s' WHERE aid = %d AND oid = %d", $image_file->filepath, $attribute_id, $option_id);
			} else {
				db_query("INSERT INTO {uc_radio_images} (aid, oid, image_path) VALUES(%d, %d, '%s')", $attribute_id, $option_id, $image_file->filepath);
			}
		    drupal_set_message('Image saved.','status');
		} else {
			drupal_set_message('Couldn\'t save file.','error');
		}

And if works great for uploading. However, the drupal error message also gets displayed if they leave the file field on the form blank (it's not a required field). But can't find how to check if the field was left empty, so I can bypass the error message.

The $form_state variable doesn't seem to have any info regarding file fields.

Any help is appreciated. Thanks!

Comments

jazzdrive3’s picture

Anyone know? I know it's probably something simple, but I can't find any examples. A print_r of the $form_state['values']['radio_image'] causes the white screen of death so no go there and trying to see if something is empty or not.

jazzdrive3’s picture

It must be something simple. How do check to see if what they entered was empty in the form_submit function?

Thanks.

Stutzer’s picture

Havind the same problem? Does anybody deals with it?

Stutzer’s picture

It seems I've got solution:

<?php

/*
 * File validation handler
 */
function uploadlight_form_elements_validate(&$form, &$form_state) {
  
  // Get from $form required for file uploading params
  $destination = file_directory_path() . $form['uploadlight_files']['destination']['#value'];
  $validators  = $form['uploadlight_files']['validators']['#value'];
  
  // Get file list from $_FILES
  $_files = $_FILES["files"]["name"];
  $file_fields = array();
  // Set $file_fields with not_empty filefields
  foreach( $_files as $_file_key => $_file ) {
    if ( $_file != '' ) $file_fields[] = $_file_key;
  }
  
  // Loop $file_fields array and upload files
  foreach ( $file_fields as $field_field ) {
    if( $file = file_save_upload( $field_field , $validators, $destination, FILE_EXISTS_RENAME ) ) {
      $form['#node']->files[$file->fid] = $file;
      $form_state['values']['files'][$file->fid] = (array)$file;
    }
  }
}

/*
 * File submission handler
 */
function uploadlight_form_elements_submit(&$form, &$form_state) {
  // Get uploaded files as objects
  $files = $form['#node']->files;
  if($files) {
    foreach ($files as $file) {
      // Save them
      file_set_status( $file , FILE_STATUS_PERMANENT );
    }
  }
}