Hi,

I tried the file uploads in my custom form using the "file" with required field option, but the system is throwing the "Required field error" even though I uploaded file.

I am using WAMPserver with PHP version 5.5+, so not a problem with the gpc_magic_quotes which should be set Off (but, this is deprecated in PHP 5.4).

Please see my code below:

function test_data_import_form($form, &$form_state) {
	$form = array();
	
	//$form['#attributes'] = array('enctype' => 'multipart/form-data');
	
	$form['import_data'] = array(
		'#type' => 'file',
		'#title' => t('Upload data file'),
		'#size' => 22,
		'#required' => TRUE,
	);
	
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Submit'),
		'#validate' => array('test_data_import_validate'),
		'#submit' => array('test_data_import_submit'),
	);
	
	return $form;
}

Comments

kirantej_p’s picture

kirantej_p’s picture

cilefen’s picture

Component: file system » forms system
Issue tags: -form, -file uploads

What is in function test_data_import_validate?

kirantej_p’s picture

Still nothing is written in the _validate function and submit function. I want to validate the input file in the test_data_import_validate file.

kirantej_p’s picture

Issue summary: View changes
anbarasan.r’s picture

File fields are not allowed to be required,
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_referen...
So, use form validation instead.

cilefen’s picture

Category: Bug report » Support request
Status: Active » Closed (works as designed)

@anbuece I think the #required element makes the field 'look' required to CSS but, yes, based on #6, this is 'works as designed'.

kirantej_p’s picture

@anbuece: File field can have the Required option. Please check the link you posted here in your comment.
@cilefen: Its not working in Drupal 7 Forms.

Ravi.J’s picture

@kiran Tej,

"Description: Indicates whether or not the element is required. This automatically validates for empty fields, and flags inputs as required. File fields are NOT allowed to be required."

Looks like you will have to implement custom validation for file field.