Tanmay Maity

Drupal: Add Custom Validation in Webform

Leave a comment

Using Webform Module we can easily create any Form in Drupal. Using Webform Validation module we can add some pre build validation such as Max length, Min length, Words blacklist, Regular expression, Specific value etc. But these are not enough, some time we need some custom validation to restrict the Webform. In this blog I will give the step by step process how to add custom validation in Webform.

To add the custom validation in Webform you have to create one Custom Module. Don’t be afraid to hear Custom Module, it is too easy, just follow the step.

Create Custom Module and Add Validation:

Step 1: Create module directory:- Go to your site’s modules directory i.e /sites/all/modules and create a directory named “custom” and inside “custom” directory create another directory named “custom_validation”. So final path of your custom module is /sites/all/modules/custom/custom_validation.

Step 2: Necessary Files:- To create any custom module minimum two files needed, these are “custom_validation.info” and “custom_validation.module” file, the file extension will be .info and .module not .php or .txt. So create those two files in custom module directory(custom_validation).

Step 3: Structure of .info file:- The .info file is the configuration file of your custom module. Each line in the .info file is a key-value pair with the key on the left and the value on the right, with an “equals sign” between them (e.g. name = custom_validation). Semicolons are used to comment out a line. “Name” and “Core” key is required for any module.

;@file: custom_validation.info
;@author: Tanmay Maity
name = Custom Validation
description = Custom module for check the validation for Webform.
package = Custom
core = 7.x

Step 4: Use of .module file:- Everything of the file will be in PHP tag (<?php ?>). In this file we will use the necessary hook. Use “hook_form_alter()” in this file to add the validation with the Webform. This hook perform before a form render. With in the hook function check the form id.

function custom_validation_form_alter(&$form, &$form_state, $form_id) {
// Check the form id
if($form_id == 'webform_client_form_4003') {
$form['#validate'][] = 'custom_validate_function';
}
}

  • The hook’s parameters contains all information of the form. If your webform is locate in “……./node/4003” then the form id will be “webform_client_form_4003” for the webform. You can echo the $form_id to check what is it.
  • If the form id match then add the custom validation with the $form parameter. “custom_validate_function” is the function name, in the function you will check all validation.

Step 5: Validation Function:- Now all set, write the validation function in the .module file. Use the function parameters $form and &$form_state to get the form field value and set the validation message.

Use &$form_state to get any field value,  “name” is the unique key of the field.

$form_state['values']['submitted']['name']

Use $form to set the error in any field, It will display the error message and the field will be highlighted. $message will be the message what you want to display.

form_error($form['submitted']['name'],$message);

Example of Validation function:

function custom_validate_function($form, &$form_state) {
if(isset($form_state['values']['submitted']['name'])) {
$name = $form_state['values']['submitted']['name'];
if( condition ) {
$message = ‘Write any message’;
form_error($form['submitted']['name'], $message);
}
}
}

Warning: It is safe to keep the validation checking with in the “isset” condition. If you are using any multi page webform using “Page break” field, Your “name” field is in 2nd page, so when you open the 1st page of the webform that time the “name” field is not present in the $form parameter, so it will through error. What “isset” will do, it will check the field is present or not, if present then will check the validation else skip the validation.
Now you can add any validation checking with any field any display the respective error. You can add multiple validation in any field also. You can add any conditional validation such as if A field’s value is this the B field’s value will be this. So feel free to add any validation.

Leave a comment