The fact that you can update any drupal form via hook_form_alter is very well known and popular thing. But what to do if we want to add an additional validation rules for the particular form? Something like function hook_validation_alter does not exists in Drupal and the hook_validate only works for node forms. Fortunately, there is an option, a bit hidden though. Therefore I would like to share the knowledge which took me a while to earn.

The trick is to add following line into the hook_form_alter:

$form['#validate'][] = 'your_validation_function';

This way you can define an array of additional validation functions. Make sure you report all validation errors using the form_set_error() function.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <css>, <drupal5>, <drupal6>, <html4strict>, <java>, <javascript>, <php>, <python>, <ruby>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.