I have a form that includes a managed_file field as well as a data popup and select field. When the user selects a date, an ajax call is made to populate the select field. However, I noticed that each time the ajax call is made, the file is uploaded, causing multiple copies of the file to to exist (each subsequent file having a number appended to the . If the user selects another date, the file is uploaded again. How do I prevent the file from being uploaded multiple times during the ajax call?
Here is my form code
function _timesdb_import_form($form,&$form_state) {
$form['file_fid'] = array(
'#type' => 'managed_file',
'#title' => 'Import File',
'#size' => 100,
'#description' => 'Select an import file.',
'#upload_location' => 'private://filelocation/',
'#required' => true,
'#default_value' => variable_get('file_fid',''),
);
$form['date'] = array(
'#type' => 'date_popup',
'#date_format' => 'm/d/Y',
'#title' => 'Start Date',
'#description' => 'Enter the start date of the meet. After changing the date, select the meet below',
'#ajax' => array(
'event' => 'change',
'callback' => '_timesdb_import_meets_ajax',
'wrapper' => 'timesdb-import-meets',
),
);
$meet_options = isset($form_state['values']['date']) ? _timesdb_import_get_meets($form_state['values']['date']) : array();
$form['meet'] = array(
'#type' => 'select',
'#title' => 'Select meet',
'#options' => $meet_options,
'#prefix' => '<div id="timesdb-import-meets">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#default_value' => 'Submit'
);
return $form;
}
The ajax callback looks like this
function _timesdb_import_meets_ajax($form,&$form_state) {
return $form['meet'];
}