=== modified file 'includes/form.inc'
--- includes/form.inc	2009-11-27 15:14:58 +0000
+++ includes/form.inc	2009-11-29 16:48:36 +0000
@@ -333,11 +333,6 @@ function drupal_rebuild_form($form_id, &
     form_set_cache($form_build_id, $form, $form_state);
   }
 
-  // Clear out all post data, as we don't want the previous step's
-  // data to pollute this one and trigger validate/submit handling,
-  // then process the form for rendering.
-  $form_state['input'] = array();
-
   // Also clear out all group associations as these might be different
   // when rerendering the form.
   $form_state['groups'] = array();
@@ -931,9 +926,11 @@ function _form_validate($elements, &$for
  */
 function form_execute_handlers($type, &$form, &$form_state) {
   $return = FALSE;
+  // If there was a button pressed, use its handlers.
   if (isset($form_state[$type . '_handlers'])) {
     $handlers = $form_state[$type . '_handlers'];
   }
+  // Otherwise, check for a form level handler.
   elseif (isset($form['#' . $type])) {
     $handlers = $form['#' . $type];
   }
@@ -1205,21 +1202,18 @@ function _form_builder_handle_input_elem
       foreach ($element['#parents'] as $parent) {
         $input = isset($input[$parent]) ? $input[$parent] : NULL;
       }
-      // If we have input for the current element, assign it to the #value property.
-      if (!$form_state['programmed'] || isset($input)) {
-        // Call #type_value to set the form value;
-        if (function_exists($value_callback)) {
-          $element['#value'] = $value_callback($element, $input, $form_state);
-        }
-        if (!isset($element['#value']) && isset($input)) {
-          $element['#value'] = $input;
-        }
+      // Call #type_value to set the form value;
+      if (function_exists($value_callback)) {
+        $element['#value'] = $value_callback($element, $input, $form_state);
       }
-      // Mark all posted values for validation.
-      if (isset($element['#value']) || (!empty($element['#required']))) {
-        $element['#needs_validation'] = TRUE;
+      if (!isset($element['#value']) && isset($input)) {
+        $element['#value'] = $input;
       }
     }
+    // Mark all posted values for validation.
+    if (isset($element['#value']) || (!empty($element['#required']))) {
+      $element['#needs_validation'] = TRUE;
+    }
     // Load defaults.
     if (!isset($element['#value'])) {
       // Call #type_value without a second argument to request default_value handling.

=== modified file 'modules/field/field.form.inc'
--- modules/field/field.form.inc	2009-11-20 04:51:27 +0000
+++ modules/field/field.form.inc	2009-11-29 16:50:07 +0000
@@ -131,6 +131,11 @@ function field_default_form($obj_type, $
  * - drag-n-drop value reordering
  */
 function field_multiple_value_form($field, $instance, $langcode, $items, &$form, &$form_state) {
+  // This form has its own multistep persistance.
+  if (!empty($form_state['rebuild'])) {
+    $form_state['input'] = array();
+  }
+
   $field_name = $field['field_name'];
 
   // Determine the number of widgets to display.

=== modified file 'modules/node/node.pages.inc'
--- modules/node/node.pages.inc	2009-11-08 10:02:41 +0000
+++ modules/node/node.pages.inc	2009-11-29 16:49:52 +0000
@@ -109,8 +109,13 @@ function node_object_prepare($node) {
  */
 function node_form($form, &$form_state, $node) {
   global $user;
+  // This form has its own multistep persistance.
+  if (!empty($form_state['rebuild'])) {
+    $form_state['input'] = array();
+  }
 
   if (isset($form_state['node'])) {
+    // Node has its own multistep persistance.
     $node = (object)($form_state['node'] + (array)$node);
   }
   if (isset($form_state['node_preview'])) {

=== modified file 'modules/simpletest/tests/ajax.test'
--- modules/simpletest/tests/ajax.test	2009-11-22 02:48:37 +0000
+++ modules/simpletest/tests/ajax.test	2009-11-29 13:08:27 +0000
@@ -141,3 +141,48 @@ class AJAXCommandsTestCase extends AJAXT
     $this->assertTrue($command['command'] == 'restripe' && $command['selector'] == '#restripe_table', "'restripe' AJAX command issued with correct selector");
   }
 }
+
+/**
+ * Test that $form_state['values'] is properly delivered to $ajax['callback'].
+ */
+class AJAXFormValuesTestCase extends AJAXTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'AJAX command form values',
+      'description' => 'Tests that form values are properly delivered to AJAX callbacks.',
+      'group' => 'AJAX',
+    );
+  }
+
+  function setUp() {
+    parent::setUp();
+
+    $this->web_user = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($this->web_user);
+  }
+
+  /**
+   * Create a simple form, then POST to system/ajax to change to it.
+   */
+  function testSimpleAJAXFormValue() {
+    // Verify form values of a select element.
+    foreach(array('red', 'green', 'blue') as $item) {
+      $edit = array(
+        'select' => $item,
+      );
+      $commands = $this->drupalPostAJAX('ajax_forms_test_get_form', $edit, 'select');
+      $data_command = $commands[2];
+      $this->assertEqual($data_command['value'], $item);
+    }
+
+    // Verify form values of a checkbox element.
+    foreach(array(FALSE, TRUE) as $item) {
+      $edit = array(
+        'checkbox' => $item,
+      );
+      $commands = $this->drupalPostAJAX('ajax_forms_test_get_form', $edit, 'checkbox');
+      $data_command = $commands[2];
+      $this->assertEqual((int) $data_command['value'], (int) $item);
+    }
+  }
+}

=== modified file 'modules/simpletest/tests/form.test'
--- modules/simpletest/tests/form.test	2009-11-18 18:51:11 +0000
+++ modules/simpletest/tests/form.test	2009-11-29 15:37:23 +0000
@@ -70,7 +70,8 @@ class FormsTestCase extends DrupalWebTes
       foreach ($data['empty_values'] as $key => $empty) {
         foreach (array(TRUE, FALSE) as $required) {
           $form_id = $this->randomName();
-          $form = $form_state = array();
+          $form = array();
+          $form_state = form_state_defaults();
           form_clear_error();
           $form['op'] = array('#type' => 'submit', '#value' => t('Submit'));
           $element = $data['element']['#title'];
@@ -131,9 +132,7 @@ class FormsTestCase extends DrupalWebTes
     $this->assertRaw(t('!name field is required.', array('!name' => 'required_checkbox')), t('A required checkbox is actually mandatory'));
 
     // Now try to submit the form correctly.
-    $this->drupalPost(NULL, array('required_checkbox' => 1), t('Submit'));
-
-    $values = json_decode($this->drupalGetContent(), TRUE);
+    $values = drupal_json_decode($this->drupalPost(NULL, array('required_checkbox' => 1), t('Submit')));
     $expected_values = array(
       'disabled_checkbox_on' => 'disabled_checkbox_on',
       'disabled_checkbox_off' => '',
@@ -413,16 +412,15 @@ class FormsFormStorageTestCase extends D
 
   function setUp() {
     parent::setUp('form_test');
+
+    $this->web_user = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($this->web_user);
   }
 
   /**
    * Tests using the form in a usual way.
    */
   function testForm() {
-
-    $user = $this->drupalCreateUser(array('access content'));
-    $this->drupalLogin($user);
-
     $this->drupalPost('form_test/form-storage', array('title' => 'new', 'value' => 'value_is_set'), 'Continue');
     $this->assertText('Form constructions: 2', t('The form has been constructed two times till now.'));
 
@@ -435,9 +433,6 @@ class FormsFormStorageTestCase extends D
    * Tests using the form with an activated $form_state['cache'] property.
    */
   function testFormCached() {
-    $user = $this->drupalCreateUser(array('access content'));
-    $this->drupalLogin($user);
-
     $this->drupalPost('form_test/form-storage', array('title' => 'new', 'value' => 'value_is_set'), 'Continue', array('query' => array('cache' => 1)));
     $this->assertText('Form constructions: 1', t('The form has been constructed one time till now.'));
 
@@ -450,12 +445,54 @@ class FormsFormStorageTestCase extends D
    * Tests validation when form storage is used.
    */
   function testValidation() {
-    $user = $this->drupalCreateUser(array('access content'));
-    $this->drupalLogin($user);
-
     $this->drupalPost('form_test/form-storage', array('title' => '', 'value' => 'value_is_set'), 'Continue');
     $this->assertPattern('/value_is_set/', t("The input values have been kept."));
   }
+
+  /**
+   * Tests updating cached form storage during form validation.
+   *
+   * If form caching is enabled and a form stores data in the form storage, then
+   * the form storage also has to be updated in case of a validation error in
+   * the form. This test re-uses the existing form for multi-step tests, but
+   * triggers a special #element_validate handler to update the form storage
+   * during form validation, while another, required element in the form
+   * triggers a form validation error.
+   */
+  function testCachedFormStorageValidation() {
+    // Request the form with 'cache' query parameter to enable form caching.
+    $this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1)));
+    // Also retrieve the form_build_id to assert that it keeps identical
+    // throughout this entire test.
+    $fields = $this->xpath($this->constructFieldXpath('name', 'form_build_id'));
+    $form_build_id = (string) $fields[0]['value'];
+
+    // Skip step 1 of the multi-step form, since the first step copies over
+    // 'title' into form storage, but we want to verify that changes in the form
+    // storage are updated in the cache during form validation.
+    $edit = array('title' => 'foo');
+    $this->drupalPost(NULL, $edit, 'Continue');
+    #$this->assertFieldByName('form_build_id', $form_build_id);
+
+    // In step 2, trigger a validation error for the required 'title' field, and
+    // post the special 'change_title' value for the 'value' field, which
+    // conditionally invokes the #element_validate handler to update the form
+    // storage.
+    $edit = array('title' => '', 'value' => 'change_title');
+    $this->drupalPost(NULL, $edit, 'Save');
+    #$this->assertFieldByName('form_build_id', $form_build_id);
+
+    // At this point, the form storage should contain updated values, but we do
+    // not see them, because the form has not been rebuilt yet due to the
+    // validation error. Post again with an arbitrary 'title' (which is only
+    // updated in form storage in step 1) and verify that the rebuilt form
+    // contains the values of the updated form storage.
+    $edit = array('title' => 'foo', 'value' => '');
+    $this->drupalPost(NULL, $edit, 'Save');
+    #$this->assertFieldByName('form_build_id', $form_build_id);
+    $this->assertFieldByName('title', 'title_changed', t('The altered form storage value was updated in cache and taken over.'));
+    $this->assertText('Title: title_changed', t('The form storage has stored the values.'));
+  }
 }
 
 /**
@@ -504,8 +541,7 @@ class FormStateValuesCleanTestCase exten
    * Tests form_state_values_clean().
    */
   function testFormStateValuesClean() {
-    $this->drupalPost('form_test/form-state-values-clean', array(), t('Submit'));
-    $values = json_decode($this->content, TRUE);
+    $values = drupal_json_decode($this->drupalPost('form_test/form-state-values-clean', array(), t('Submit')));
 
     // Setup the expected result.
     $result = array(
@@ -533,3 +569,36 @@ class FormStateValuesCleanTestCase exten
   }
 }
 
+/**
+ * Tests form rebuilding.
+ *
+ * @todo Add tests for other aspects of form rebuilding.
+ */
+class FormsRebuildTestCase extends DrupalWebTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Form rebuilding',
+      'description' => 'Tests functionality of drupal_rebuild_form().',
+      'group' => 'Form API',
+    );
+  }
+
+  function setUp() {
+    parent::setUp('form_test');
+
+    $this->web_user = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($this->web_user);
+  }
+
+  /**
+   * Tests preservation of values during an "add another item" operation.
+   */
+  function testRebuildPreservesValues() {
+    $edit = array(
+      'grocery' => 'apple',
+    );
+    $this->drupalPost('form-test/form-rebuild-preserve-values', $edit, 'Submit');
+
+    $this->assertFieldByName('grocery', 'apple', t('Item value kept in a rebuild.'));
+  }
+}

=== modified file 'modules/simpletest/tests/form_test.module'
--- modules/simpletest/tests/form_test.module	2009-11-21 17:01:31 +0000
+++ modules/simpletest/tests/form_test.module	2009-11-29 16:44:53 +0000
@@ -49,7 +49,7 @@ function form_test_menu() {
   $items['form_test/form-storage'] = array(
     'title' => 'Form storage test',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('form_storage_test_form'),
+    'page arguments' => array('form_test_storage_form'),
     'access arguments' => array('access content'),
     'type' => MENU_CALLBACK,
   );
@@ -78,6 +78,14 @@ function form_test_menu() {
     'type' => MENU_CALLBACK,
   );
 
+  $items['form-test/form-rebuild-preserve-values'] = array(
+    'title' => 'Form values preservation during rebuild test',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('form_test_form_rebuild_preserve_values_form'),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+
   return $items;
 }
 
@@ -280,9 +288,12 @@ function form_test_mock_form_submit($for
  * request parameter "cache" the form can be tested with caching enabled, as
  * it would be the case, if the form would contain some #ajax callbacks.
  *
- * @see form_storage_test_form_submit().
+ * @see form_test_storage_form_submit().
  */
-function form_storage_test_form($form, &$form_state) {
+function form_test_storage_form($form, &$form_state) {
+  if (!empty($form_state['rebuild'])) {
+    $form_state['input'] = array();
+  }
   // Initialize
   if (empty($form_state['storage'])) {
     if (empty($form_state['input'])) {
@@ -301,25 +312,29 @@ function form_storage_test_form($form, &
   // Count how often the form is constructed
   $_SESSION['constructions']++;
 
+  $form['title'] = array(
+    '#type' => 'textfield',
+    '#title' => 'Title',
+    '#default_value' => $form_state['storage']['thing']['title'],
+    '#required' => TRUE,
+  );
+  $form['value'] = array(
+    '#type' => 'textfield',
+    '#title' => 'Value',
+    '#default_value' => $form_state['storage']['thing']['value'],
+    '#element_validate' => array('form_test_storage_element_validate_value_cached'),
+  );
   if ($form_state['storage']['step'] == 1) {
-    $form['title'] = array(
-      '#type' => 'textfield',
-      '#title' => 'title',
-      '#default_value' => $form_state['storage']['thing']['title'],
-      '#required' => TRUE,
-    );
-    $form['value'] = array(
-      '#type' => 'textfield',
-      '#title' => 'value',
-      '#default_value' => $form_state['storage']['thing']['value'],
-    );
     $form['submit'] = array(
       '#type' => 'submit',
       '#value' => 'Continue',
     );
   }
   else {
-    $form['body'] = array('#value' => 'This is the second step.');
+    $form['body'] = array(
+      '#type' => 'item',
+      '#value' => 'This is the second step.',
+    );
     $form['submit'] = array(
       '#type' => 'submit',
       '#value' => 'Save',
@@ -336,9 +351,27 @@ function form_storage_test_form($form, &
 }
 
 /**
- * Multistep form submit callback.
+ * Form element validation handler for 'value' element in form_test_storage_form().
+ *
+ * Tests updating of cached form storage during validation.
  */
-function form_storage_test_form_submit($form, &$form_state) {
+function form_test_storage_element_validate_value_cached($element, &$form_state) {
+  // If caching is enabled and we receive a certain value, change the value of
+  // 'title'. This presumes that another submitted form value triggers a
+  // validation error elsewhere in the form. Form API should still update the
+  // cached form storage though.
+  if (isset($_REQUEST['cache']) && $form_state['values']['value'] == 'change_title') {
+    $form_state['storage']['thing']['title'] = 'title_changed';
+    // @todo This really shouldn't be necessary. Should Form API cache that a
+    //   form can be cached? See http://drupal.org/node/641356
+    $form_state['cache'] = TRUE;
+  }
+}
+
+/**
+ * Form submit handler for form_test_storage_form().
+ */
+function form_test_storage_form_submit($form, &$form_state) {
   if ($form_state['storage']['step'] == 1) {
     $form_state['storage']['thing']['title'] = $form_state['values']['title'];
     $form_state['storage']['thing']['value'] = $form_state['values']['value'];
@@ -477,3 +510,29 @@ function _form_test_checkbox_submit($for
   drupal_json_output($form_state['values']);
   exit();
 }
+
+/**
+ * Form builder for testing preservation of values during a rebuild.
+ *
+ * Creates a form with two "add another item" buttons, each controlling a
+ * different element. Used to test that a rebuilt form has the expected values.
+ */
+function form_test_form_rebuild_preserve_values_form($form, &$form_state) {
+  $form['grocery'] = array(
+    '#type' => 'textfield',
+    '#title' => 'Grocery item',
+    '#default_value' => 'DEFAULT',
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Submit',
+  );
+  return $form;
+}
+
+/**
+ * Form submit handler for form_test_form_rebuild_preserve_values_form().
+ */
+function form_test_form_rebuild_preserve_values_form_submit($form, &$form_state) {
+  $form_state['rebuild'] = TRUE;
+}

