views:

1413

answers:

3

I have a Drupal module page where I am populating a form with a drop-down that contains a list of available parts of a set of files that the user can upload. Once the user uploads a file of a certain type, it removes that option from the list, and when all the available files are uploaded the form will not be rendered.

The problem is that Drupal is drawing the form before it carries out the submit action, so it appears to the user that the operation didn't work until they reload the page.

What is the common way to deal with this? Setting form_state['redirect'] to go to the page doesn't seem to work.

A: 

The problem is that Drupal is drawing the form before it carries out the submit action, so it appears to the user that the operation didn't work until they reload the page.

I cannot believe it. Are sure about that?

EricSchaefer
Yes, the basic flow is shown here: http://www.flickr.com/photos/thecancerus/2394730671/sizes/o/
alxp
The illustration you pointed to contradicts what you just said -- the form is NOT rendered to HTML until after the submit functions are run. You're confusing the process of building the form (i.e., defining what fields are in it) with the rendering to HTML.
Eaton
+3  A: 

You modify your form so that it saves some information on the state of the form. Then you add a new case to the beginning of the submit function that returns immediately if you're not done uploading all the files, and it will redraw the form.

function modulename_uploader_form(&$form_stuff=null) {
//the function that sets your form array
$stuff = (isset($form_stuff['values'])) ? 
    $form_stuff['storage']['done_uploading'] : 
    false; //if values isnt set, this is the first visit. 
$form_stuff['storage']['done_uploading'] = done_uploading();
.... the rest of your form creation function.


function modulename_uploader_submit($form, &$form_stuff) {
    if($form_stuff['storage']['done_uploading']) {
return;
}
... rest of the function

make sure to unset the storage variable when you're done processing the form. You can also google multi page forms in drupal.

jacobangel
+1  A: 

Setting $form_state['redirect'] in your submit handler will just cause the form to reload, fresh, without the old data. It's a way of clearing out the form so that old values don't hang around as defaults.

You probably want to use $form_state['rebuild'], which gives your form-building functions an opportunity to rebuild the form's actual structure, adding more fields or removing options from other fields after the form submit handlers run.

This blog post has a rough tutorial on doing multiple-stage forms in D6, and the Drupal 5 to 6 upgrade docs on Drupal.org contain a useful overview of how $form_state works and what its various flags are for.

Eaton