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...
$form['field_stocklist'][LANGUAGE_NONE][0]['fid'][#upload_validators']['stocklist_validation'] = array();
or similar