0

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'];
}
0

Drupal submits the entire form at every ajax call, I think you'd have to use hooks before submit, before send or similar to prevent it. Heres something to get you started. Or simply put the file field last or at least after the ajax form elements.

Disable all form elements when doing AJAX

Ideally drupal should allow you to specify what you want to submit. please share your solution if you figure it out.

  • Thanks for the input. I posted my solution, which basically isn't a solution so conceding the solution to Drupal's way of doing things. – Robbert Oct 9 '13 at 20:35
0

I ended up returning the entire form. I moved the prefix and suffix to the form element, so the entire form is replaced after the ajax call. I figured, if I can't beat it, join it :).

$form = array(
    '#prefix' => '<div id="timesdb-import-meets">',
    '#suffix' => '</div>',
);
$form['file_fid'] = array(
    //'#name' => 'files[importfile]',
    '#type' => 'managed_file',
    '#title' => 'Import File',
    '#size' => 100,
    '#description' => 'Select an import file.  Supported types are CSV from SWIMS or Commlink',
    '#upload_location' => 'private://timesdatabase/',
    '#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,

);

$form['submit'] = array(
    '#type' => 'submit',
    '#default_value' => 'Submit'
);
return $form;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.