4

I need my users to upload a csv, using a cck filefield. I then wish to confirm they have the correct order of columns.

My research led me to believe that writing a custom validator for the filefield, was the best option. I have used this as a resource: https://drupal.org/node/546146 for writing it...

I then used this for an example of how to open a csv and read it: http://oliverhuynh.wordpress.com/2011/06/21/drupal-batch-csv-importing-template/ ...

I'm very new to this and lack the insights to spot what i'm doing wrong... Here's my code:

function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'stocklist_node_form') {     

    $form['field_stocklist'][0]['#upload_validators']['stocklist_validation'] = array();
    return $form;
  }

}

/**
 * The validation for the file.
 *
 */

function stocklist_validation($field) {
  // variable for error messages
  $errors = array();


  // do some processing on the field / file
  $filepath = $field->filepath;
  global $base_url;
  $fileurl = $base_url . '/' . $file_path;
 // Open the file
      if ( $handle = fopen($fileurl, 'r') ) {
        $line_count = 1 ;
        $first = TRUE ;
        if ( $line = fgetcsv($handle, 4096) ) {

        // Validate the headers 

         if ( $line[0] !== 'manufacturer' ) {
            $errors[] = t('Unable to verify uploaded file !filepath. Please ensure the file has the correct file type, column names and information.', array('!filepath' => $field->filename));
         }
        }
        fclose($handle);
      }


// in case of error, add error message

          return $errors;
}

I would really appreciate any help or pointers... I feel I'm one step away from finishing it but running out of google searches... It doesn't matter whether I upload a valid or invalid file, it just throws the defined error message... Why is it doing that?

edit:

Here is a screenshot using devel whilst viewing a 'stocklist' type node. Thought it may help... enter image description here

  • Hello and welcome :) This site is Q&A, not a forum. Please ask "how to do X?" or "why Y fails?". Just, well, clear questions. On a forum your post would be a nice start of a validator thread. But that's not what we do here. – Mołot Dec 20 '13 at 11:22
  • Hi Molot... Is this better? Thanks for helping me understand things better... – gMaximus Dec 20 '13 at 11:42
  • Better :) Not yet perfect, but yes, better. – Mołot Dec 20 '13 at 11:43
  • Just made one last change to the end of it... – gMaximus Dec 20 '13 at 11:46
  • Have a peek at this, it should help. The validation callback probably needs to be attached like $form['field_stocklist'][LANGUAGE_NONE][0]['fid'][#upload_validators']['stocklist_validation'] = array(); or similar – Clive Dec 20 '13 at 13:03
0

Just got this too work:

/**
 * The validation for the stocklist.
 *
 */

function stocklist_validation($field) {
  // variable for error messages
  $errors = array();

  // do some processing on the field / file
  $filepath = $field->filepath;

 // Open the file

      if ( $handle = fopen($filepath, 'r') ) {
        $line_count = 1 ;
        $first = TRUE ;
        if ( $line = fgetcsv($handle, 4096) ) {

        // Validate the headers 
         if ( $line[0] !== 'something' ) {
            $errors[] = t('Please ensure the files 1st column has the something in it. Including something as the heading.'); 
         }

    if ( $line[1] !== 'somethingelse' ) {
            $errors[] = t('Please ensure the files 2nd column has the somethingelse in it. Including somethingelse as the heading.'); 
         }


        fclose($handle);
      }

           else {
        $errors[] = t('Unable to open uploaded file !filepath.', array('!filepath' => $filepath));   
       }
      } 
// in case of errors, return error messages

          return $errors;
}

The first function didn't need changing...

In the second function, it was the fopen() that needed changing. Secondly, like an idiot I just realised that the "valid" file was actually tab delimited... Doh! Would be nice to be able to accept those files too. As some software saves csv files in that format, such as the one I clearly used, OpenOffice.

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.