On this page
Adding custom validation rules - Webform Validation hooks
This documentation needs review. See "Help improve this page" in the sidebar.
This page describes how to write custom validation rules for your webforms. This requires installation of the Webform and Webform Validation modules. Drupal 7 only.
The following code should be implemented in your own custom module.
Adding custom validation rules
The following steps will let you add custom validators through your module:
1/ Implement hook_webform_validation_validators().
This hook implementation should return an array of validator key => options array entries.
The options array can contain the following configuration keys:
- name (required): name of the validator
- component types (required): defines which component types can be validated by this validator. Specify 'all' to allow all types
- custom_error (optional): define whether a user can specify a custom error message upon creating the validation rule.
- custom_data (optional): define whether custom data can be added to the validation rule
- min_components (optional): define the minimum number of components to be selected for creating a validation rule
- max_components (optional): define the maximum number of components to be selected for creating a validation rule
- description (optional): provide a descriptive explanation about the validator
See function webform_validation_webform_validation_validators() in webform_validation.validators.inc for a live example
2/ Implement hook hook_webform_validation_validate($validator_name, $items, $components, $rule).
This hook gets passed 4 parameters, which will allow you to react to your custom validator (or any other validator for that matter).
Explanation about these parameters:
- $validator_name: this is the validator name (i.e. array key as entered in hook_webform_validation_validators)
- $items: array containing user submitted entries to be validated.
- $components: this array contains the definitions of the webform components in your form
- $rule: this array contains the details of your validation rule
See function webform_validation_webform_validation_validate() in webform_validation.validators.inc for a live example
Additional hooks
The hook hook_webform_validation($type, $op, $data) can be used to react on various webform_validation based actions.`
- $type - possible values: 'rule'
- $op - possible values: 'add', 'edit', 'delete'
- $data - array with rule data in case of $op add/edit, rule id in case of $op delete.
The hook hook_webform_validator_alter(&$validators) can be used to alter the array of validators that is being generated by hook_webform_validation_validators().
- $validators - array of validators as supplied by modules implementing hook_webform_validation_validators().
Example
/**
* Implements hook_webform_validation_validators().
*/
function idvalidation_webform_validation_validators() {
return array(
'validate_id' => array(
'name' => "Validate ID",
'description' => "Validate ID Description",
'component_types' => array(
'textfield',
),
)
);
}
/**
* Implements hook_webform_validation_validate().
*/
function idvalidation_webform_validation_validate($validator_name, $items, $components, $rule) {
if ($items) {
switch ($validator_name) {
case 'validate_id':
$valid_ids = array(1, 2, 3); // get this from database source instead
$errors = array();
foreach ($items as $key => $val) {
if ($val && (!in_array($val, $valid_ids))) {
$errors[$key] = t('%item is not a valid ID', array('%item' => $components[$key]['name']));
}
}
return $errors;
break;
}
}
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion