For some reason, my form breaks when I try to make a file upload required. Here is the code for it:
$form_id = "upload_form";
$form[$form_id] = array (
'#type' => 'fieldset',
'#description' => t('This is a utility to import nodes from a Comma Separated Value file. To begin, pick a node type, and upload a CSV.'),
);
$form[$form_id]['type'] = array(
'#title' => t('Enter node type'),
'#type' => 'textfield',
// '#autocomplete_path' => '', TODO: autocomplete for node types
'#required' => TRUE,
'#description' => t('This node type should already exist. If it doesn\'t, create it first.'),
);
$form[$form_id]['upload'] = array(
'#type' => 'file',
'#title' => t('Upload CSV file'),
// '#size' => 40,
'#description' => t('This will not work for a non-CSV file.'),
// '#required' => TRUE, TODO: breaks it. why?
);
$form[$form_id]['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['#attributes'] = array('enctype' => 'multipart/form-data');
On a Drupal support site, someone says that it's impossible to make file uploads required. Is this true?
This is my workaround to make file field required:
I'm not a Drupal expert but you could check if the
$_FILES
variable exists, no?