Hi folks,

I'm trying to put together a module in Drupal 6, and I'm confused by the forms API. I have a form field that's pre-populated with a value.

If I view the form, change the value, and submit it, then inspect the $form_state array in my form_submit() function, $form_state['values']['fieldname'] contains the original pre-populated value rather than the POSTed value that the API docs suggest it will have.

On the other hand, $form_state['clicked_button']['#post']['fieldname'] appears to contain the POSTed value.

Is it safe to use this instead? Is $form_state['values'] supposed to have been validated? Am I doing something terribly, horribly wrong?

Thanks for your help!

Comments

greenbeans’s picture

I was setting #values instead of #default_values in the form definition, so they were getting overridden on each submission.

gaxze’s picture

could you possibly offer any code examples? i have this exact problem. see here

gaxze’s picture

Ahh ive worked it out.. had a brain fart yesterday it seems.

sridharpandu’s picture

I am having a similar problem $form_state doesn't retain its values when traversing to the next page. Would appreciate if you could post a note on how you solved it.

Acer Aspire 5745
[i5 430M, 3GB, 320GB]
Ubuntu 12.04 (Precise Pangolin)
Drupal 6.15, 7.x
DigitalOcean, Go Daddy, Rackspace,

TheCortex’s picture

// Modifies this function so that it will respond appropriately based on
// which page was submitted. If the first page is being submitted,
// values in the 'storage' array are saved and the form gets 
// automatically reloaded.
// If page 2 was submitted, we display a message and redirect the 
// user to another page. 
function my_module_my_form_submit($form, &$form_state) {
  // Handle page 1 submissions
  if ($form_state['clicked_button']['#id'] == 'edit-next') {
    $form_state['storage']['page_two'] = TRUE; // We set this to determine
                                               // which elements to display
                                               // when the page reloads.
                                               
    // Values below in the $form_state['storage'] array are saved 
    // to carry forward to subsequent pages in the form.
    $form_state['storage']['page_one_values'] = $form_state['values'];
  }
  // Handle page 2 submissions
  else {
    /* 
     Normally, some code would go here to alter the database with the data
     collected from the form. Sets a message with drupal_set_message()
     to validate working code.
     */
    drupal_set_message('Your form has been submitted');
    unset ($form_state['storage']); // This value must be unset for 
                                    // redirection! This is because
                                    // $form_state['rebuild'] gets set to TRUE
                                    // when 'storage' is set. See code sample
                                    // #9 for more on this.
                                    
    $form_state['redirect'] = 'node'; // Redirects the user.
  }
}