alxp
alxp

Reputation: 6331

Drupal form being rendered before submit action

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.

Upvotes: 1

Views: 2601

Answers (3)

Eaton
Eaton

Reputation: 7415

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.

Upvotes: 1

jacobangel
jacobangel

Reputation: 6996

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.

Upvotes: 3

EricSchaefer
EricSchaefer

Reputation: 26370

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?

Upvotes: 0

Related Questions