Adding Validation to Drupal 7 Contact Form

On your Drupal site, you may have the problem I have found. I wanted to do additional validation of contact form email before allowing the visitor to submit email. Your site may need this to protect against spam, or just to assure you are getting information that you need in the email. How do your add validation to a Drupal contact form?

Solution: You can create a module that will solve your problem, and the process is not complicate.

This post explains the adding of validation to a standard Drupal 7 site wide contact form. The post assumes that you know how to create a basic module, you know where to place custom modules, and the article restricts itself to the actual hooks needed to complete the functionality.

For completeness I will mention the creation of the module info file in passing, without much explanation. Create the file mailFilter.info, containing the following information:



  name = Mail Filter
  description = Offering a filter to your email needs.
  package = Core
  version = VERSION
  core = 7.x
  ; Information added by drupal.org packaging 
  version = "7.0-1.0-DEV"
  datestamp = "1399428226"

Create a file mailFilter.module to contain the actual validation functionality. Adding the actual validation requires 3 steps. The steps are:

  • Look at the form and determine its form_id
  • Use hook_form_alter to include the validation
  • Create the validation function

To find the form_id,

  • Add the following to your mailFilter.module
    
      function mailFilter_form_alter($form, &$form_state, $form_id) {
         echo $form_id ;
      }
    
  • Go to module administration and activate mailFilter
  • Clear your cache
  • Go onto your site and navigate to the form you want to add validated. For each form you pass, the form_id will be printed at the top of your page. When you get to the form you want, you will know the form_id. If you are planning to add validation to the main site wide contact form, you will find the form_id=”contact_site_form.”

Use mailFilter_form_alter to include the validation

Now that you know the form name, alter the mailFilter_form_alter to perform a more useful function. Ask form_alter to add the validation to the list of existing validation. To do that, alter the mailFilter_form_alter as follows:



  function mailFilter_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == "contact_site_form") {
       $form['#validate'][] = 'mailFilter_form_validate' ;
    }
  }    

The site will now want to do additional validate, as defined in mailFilter_form_validate module.

Create the validation function

Now, you must create the validation function. The validation function may do anything you like. In this sample, the validation will look for specific garbage frequently found in spam submitted from websites. When the spam marker is detected, the mail will be rejected. Validation will be included in mailFilter_form_validate as follows:



  function mailFilter_form_validate($form, &$form_state) {
    $msg = $form_state['input']['message'] ;
    $pos = strpos($msg, "[/url]") ;
    if ($pos !== false) {
      form_set_error('message', t('Due to spam problems, you may not include [url] tags in your email.'));
    }
    $pos = strpos($msg, "[/link]") ;
    if ($pos !== false) {
        form_set_error('message', t('Due to spam problems, you may not include [link] tags in your email.'));
    }  
  }

Using the above processing, those automated emails that include reference to “[/url]” or “[/link]” will be rejected by the mail processing. However, you can change this code to anything that helps you validate your input.

Hope this helps you.

Leave a Reply