Drupal7 custom form validation function

Sometimes I need a custom form validation to tamper user input. This is quite easy to accomplish, you just need to add your custom validation function to the forms validate array:

function your_module_form_alter(&$form, $form_state, $form_id) {
  switch($form_id) {
    case 'webform_client_form_907': {
      array_unshift($form['#validate'], 'set_email_text');
    } break;
  }
}

Then you just need to populate your custom validate function like so:

function set_email_text($form, &$form_state) {
 $form_state['values']['submitted']['email_text'] = "Some value."
}
This entry was posted in CMS, Drupal. Bookmark the permalink.

Leave a comment