How do you modify the form after it validates and show's errors I have built a form using CCK in Drupal 6. I have two fields, one which is called health plans and the other is called terms. If I select a health plan I update the terms via AJAX (both are select lists).

So for example: (health_plan_field)Plan A -> (terms_field)(1, 3, 6, 12)

I submit the form and serialize the terms inside a session so that I can keep them.

My problem is that if the form has errors, the terms field is blank because it starts empty until a health plan is selected. So if I have already selected a plan and terms, when the form returns after errors I have nothing.

I have tried to use form_alter (have used this before) by recursively walking down the cck fields on $form to modify the term_field options based on what I have in the session. This does nothing on return.

My code is as follows (called from hook form alter):

function acg_pportal_new_service_order_form_cck_walkerTerms(&amp;$array, &amp;$cck_fields) {

$terms = unserialize($_SESSION['termDataFromForm']);
if (!empty($terms)){
    foreach($array as $key =&gt; &amp;$value) {
        if (in_array($key, $cck_fields)) {
        unset($value['value']['#options']);
        $first = "";
        foreach($terms as $key2 =&gt; $value2){
            if (empty($first)){
                $first = $key2;
            }
            $value['value']['#options'][$key2] = $value2;
        }
            $value['value']['#default_value'] = $first;
            $value['value']['#value'] = $first;
        } 

    }
    }
}


Is there anything that you know that will help me in this case.
You need to set the form to rebuild on validation fail.

$form['#rebuild'] = TRUE;   // cause form rebuild after validation failed 


Then you can use your $form_state['values'] inside your hook_form_alter().

OR

You can create a custom function and use the #after_build property.