1

I'm trying to make a simple form to upload a txt/csv file, in order to parse it and update some values in database. Here's my code. Unfortunately, at the moment using the form nothing happens (no folder creation, no file upload...) Here's the code. What can be wrong here? Thank you

function vts_departs_conf_form() {
  $form = array();

  $form['browser'] = array(
    '#type' => 'fieldset',
    '#title' => t('Send file'),
    '#collapsible' => TRUE,
    '#description' => t("Send foo file."),
  );

  $file_size = t('Maximum file size: !size MB.', array('!size' => file_upload_max_size()));

  $form['browser']['file_upload'] = array(
    '#type' => 'file',
    '#title' => t('CSV File'),
    '#size' => 40,
    '#description' => t('Select the CSV file to be imported. ') . $file_size,
  );

  $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Save'),
  );

  $form['#attributes']['enctype'] = "multipart/form-data";

  return $form;
}

function vts_departs_conf_form_validate($form, &$form_state) {
  // attempt to save the uploaded file
  $filepath = 'public://depconfs';
  file_prepare_directory($filepath, FILE_CREATE_DIRECTORY);
  $file = file_save_upload('file_upload', array('file_validate_extensions' => array('CSV TXT')), $filepath, FILE_EXISTS_REPLACE);

  // check file uploaded OK
  if (!$file) { 
    form_set_error('file_upload', t('A file must be uploaded or selected from FTP updates.'));
  }
  else {
    // set files to form_state, to process when form is submitted
    $form_state['values']['file_upload'] = $file;
  }
}

function vts_departs_conf_form_submit($form, &$form_state) {
  //Prepare file parsing
  $line_max = variable_get('user_import_line_max', 1000);
  ini_set('auto_detect_line_endings', true);
  $filepath = $form_state['values']['file_upload']->filepath;
  $handle = @fopen($filepath, "r");

  while ($row = fgetcsv($handle, $line_max, ',')) {
    // do stuff
  }
}

EDIT :

As a first step, @bdurbin corrected my naming mistakes.

Reading file_save_upload() API description, I realised that my basic validation checks are already performed, so I removed them.

So final is quite lighter :

function vts_departs_conf_form_validate($form, &$form_state) {

  $filepath = 'public://depconf/';
  $validators = array('file_validate_extensions' => array('csv txt'));
  $file = file_save_upload('file_upload', $validators , $filepath, FILE_EXISTS_REPLACE);
  //push file object to submit step 
  $form_state['values']['file_upload'] = $file;

}
  • In the last line of EDIT part, change $file with $file->destination for getting correct result – WaQaR Ali Jan 12 '16 at 7:39
1

Drupal's naming convention for the automagic association of validation and submit handlers for a given form is:

  • name_of_form() (builder)
  • name_of_form_validate() (validation handler)
  • name_of_form_submit() (submit handler)

In your case, if you wanted to name your form vts_departs_conf_form(), you'd want to either rename your validation handler to vts_departs_conf_form_validate() and your submit handler to vts_departs_conf_form_submit(), or use the #validate and #submit form attributes to associate functions that don't follow the naming conventions.

In your example, changing the builder name to vts_departs_conf() made the form work as expected.

  • Super, stupid I am ! But got a new error now : A file must be uploaded or selected from FTP updates. Any idea ? Thank you – Kojo Oct 5 '15 at 16:03
  • 1
    I didn't run into that validation error in a local test, which suggests that your file_prepare_directory() call might not be succeeding, possibly due to local filesystem permissions? I'd suggest checking your return value for that call in vts_departs_conf_validate() and sending back a validation error if it can't prepare the destination directory for the upload. – bdurbin Oct 5 '15 at 16:16
  • Glad to hear that you found additional information. If you're satisfied that my answer solved your initial question (why the validation and submit handlers weren't firing), please be sure to mark the answer as accepted. – bdurbin Oct 5 '15 at 21:57

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.