Unable to upload files with 'file' form field, Ajax and AHAH helper modules

cyb - September 20, 2009 - 14:45
Project:AHAH helper
Version:6.x-2.0
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

I'm writing a Drupal 6 module from scratch that needs uploading files in Ajax and I'm apparently hit by an incompatibility between Ajax, AHAH helper and 'file' type forms fields.

Here is the code that reproduces the issue:

<?php
function cdp_perm() {
    return array(
'access CDP application form');
}

function
cdp_menu() {

   
$items = array();

   
$items['cdp/application'] = array(
       
'title' => t('Application form'),
       
'page callback'    => 'drupal_get_form',
       
'page arguments'   => array('cdp_application_form'),
       
'access callback'  => TRUE,
       
'access arguments' => array('access CDP application form'),
       
'type' => MENU_CALLBACK
   
);

    return
$items;
}

function
cdp_application_form($form_state) {

   
$form=array();

   
ahah_helper_register($form, $form_state);

   
$form['#ajax'] = array('enabled' => TRUE);

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

   
$form['visual'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('Visual of the project'),
       
'#prefix' => '<div id="visual-wrapper">',
       
'#suffix' => '</div>',
       
'#tree'   => TRUE,
    );

   
$form['visual']['file'] = array(
       
'#type' => 'file',
       
'#title' => t('File'),
    );

   
$form['visual']['upload'] = array(
       
'#type'  => 'submit',
       
'#value' => t('Upload'),
       
'#submit' => array('cdp_uploadhack_submit'),
    );

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

function
cdp_application_form_submit($form, &$form_state) {
 
drupal_set_message(t('You have successfully submitted your project.'));
}

function
cdp_uploadhack_submit($form, &$form_state) {
   
$form_state['rebuild'] = TRUE;

    if(
$file=file_save_upload('visual')) {
       
drupal_set_message(t('upload ok'));
    }
    else {
       
drupal_set_message(t('Nothing uploaded'), 'warning');
    }


}
?>

The upload form is broken, you get the extroardinary helpful error message "ajax.module: An unknown error has occurred.".

If you comment « $form['#ajax'] = array('enabled' => TRUE); », it just work but Ajax is indeed not supported anymore for error reporting.

It look related to http://drupal.org/node/399676 but the work-around does not work.

If anyone know how to fix it, I would be thankful.

Regards,

#1

pobster - September 20, 2009 - 19:31

The issue is likely that you're not following the instructions at all for ahah_helper. Try taking a look at the example module given; http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/ahah_helper...

You're supposed to use '#path' => ... to allow ahah_helper to do its magic.

Pobster

#2

jbrown - September 20, 2009 - 19:32

I installed your module and it is working just fine on my local D6 install.

Are you able to replicate the problem on a publicly visible site so I could take a look?

Have you tried testing with smaller files?

#3

brendoncrawford - September 21, 2009 - 08:10

I am the author of the AJAX module. AFAIK, the Ajax module and AHAH do not play nicely together. Getting them to do so, might be a fairly large task.

#4

pavaniakella - November 30, 2009 - 10:37

Hi,

Did u get any solution Now I am facing the same problem.
Please help me if u get any solution..

Regards,
Pavani

 
 

Drupal is a registered trademark of Dries Buytaert.