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(&$array, &$cck_fields) {

$terms = unserialize($_SESSION['termDataFromForm']);
if (!empty($terms)){
    foreach($array as $key => &$value) {
        if (in_array($key, $cck_fields)) {
        $first = "";
        foreach($terms as $key2 => $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().


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

  • Just to check, this is where I have placed the line of code you mentioned, in the hook_form_alter. $form['rebuild'] = TRUE; $form['#after_build'][] = 'acg_pportal_new_service_order_form_after_build'; $form['#validate'][] = 'acg_pportal_new_service_order_form_custom_validate'; $form['#submit'][] = 'acg_pportal_new_service_order_form_custom_submit';
    – Usman
    Mar 5 '13 at 9:04
  • I would do either or, if you just do $form['rebuild'] = TRUE then your hook_form_alter() will get called again instead of serving your form up through cache. Then you can do some checks in their based on validation errors. Print out your $form_state['values'] in your hook. Then view the form and then add some stuff to your inputs and make it fail validation. See the difference.
    – iLLin
    Mar 5 '13 at 14:19

hook_form_alter() gets called whenever a form is build or rebuilt, meaning it'll get called again when the form fails validation. At this point you can look at the $form_state variable to see the current state of the form and do whatever you need to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.