Erich H.
Erich H.

Reputation: 467

Altering a Drupal form after validation

I have a Drupal 7 form where after submitting it, some validation happens. It is taking the email address and doing a database look-up to see if that user already exists. If the user exists, I need to alter the form that re-renders on the page that normally displays the errors, removing some fields. Basically on the error page, regardless of any other validation errors they would have normally received (First name required, last name required etc.) they would only get one error message that says "that email address is already in the system" and then I no longer want to display ANY of the other fields at this point except the email address field and a file upload field. So I'm having trouble trying to figure out how to alter the form after the first submission based on some validation.

Thanks

Upvotes: 1

Views: 1273

Answers (1)

daggerhart
daggerhart

Reputation: 491

What you want to do is add some data to the $form_state variable in your validation function that can inform your form function as to what fields it should provide.

Untested Example:

    function my_form($form, &$form_state){
      $form['my_field1'] = array('#markup' => 'my default field');

      // look for custom form_state variable
      if ($form_state['change_fields']) {
        $form['my_field2'] = array('#markup' => 'my new field');
      }
    }

    function my_form_validate($form, &$form_state){
      // if not valid for some reason {
        form_set_error('my_field1','this did not validate');
        $form_state['change_fields'] = true;
      // }
    }

Upvotes: 0

Related Questions