This module drastically increase the validation power of Drupal Form API (FAPI). The core form API has no built in validators available to you, nor filters. With this module you now have access to many basic and useful filters, helping you with what should be simple form validation

You can use the existent filters and rules or create your own. It is up to you.

Please note that this is under active development, and any new rules or filters will be documented in the NEW readme.txt file.


Available Rules

Rule Usage Description
numeric numeric Must contains only numbers.
alpha alpha Must contains only alpha characters.
length length[<total>]
length[<min>, <max>]
length[<min>, *]
chars chars[<char 1>, <char 2>, ..., <char N>] Accept only specified characters.
email email Valid email
url url
url[absolute]
Valid URL. If absolute parameter is specified, the field value must have to be a full URL.
ipv4 ipv4 Valid IPv4
alpha_numeric alpha_numeric Accept only Alpha Numeric characters
alpha_dash alpha_dash Accept only Alpha characters and Dash ( - )
digit digit Checks wheter a string consists of digits only (no dots or dashes).
decimal decimal
decimal[<digits>,<decimals>]
regexp regexp[/^regular expression$/] PCRE Regular Expression
match_field match_field[otherfield] Check if the field has same value of otherfield.
range range[<min>, <max>] Check if the field value is in defined range.

Available Filters

Filter Description
numeric Remove all non numeric characters.
trim Remove all spaces before and after value.
uppercase Transform all characters to upper case.
lowercase Transform all characters to lower case.
strip_tags Strips out ALL html tags.
html_entities Decodes all previously encoded entities, and then encodes all entities.

Usage

Example:

<?php
//...
$form['myfield'] = array(
 
'#type' => 'textfield',
 
'#title' => 'My Field',
 
'#required' => TRUE,
 
'#rules' => array(
   
'email',
   
'length[10, 50]',
    array(
'rule' => 'alpha_numeric', 'error' => 'Please, use only alpha numeric characters at %field.'),
    array(
'rule' => 'match_field[otherfield]', 'error callback' => 'mymodule_validation_error_msg'),
  ),
 
'#filters' => array('trim', 'uppercase')
);
//...
function mymodule_validation_error_msg($rule, $params, $element, $form_state) {
  return
t("My custom error message for %field", array("%field" => $element['#title']));
}
?>

Developer

If any of this rules was what you need, you can use your own with hook_fapi_validation_rules and hook_fapi_validation_filters.

<?php
/**
* Implementation of hook_fapi_validation_rules
*/
function mymodule_fapi_validation_rules() {
  return array(
   
'rule_name' => array(
     
'callback' => 'mymodule_validation_rule_name',
     
'error_msg' => 'Invalid value for %field'
   
),
   
'other_rule' => array(
     
'callback' => 'mymodule_validation_other_rule',
     
'error_msg' => "Don't use this value at field %field"
   
),
  );
}
/**
* Implementation of hook_fapi_validation_filters
*/
function mymodule_fapi_validation_filters() {
  return array(
   
'filter_name' => array(
     
'callback' => 'mymodule_validation_filter_name',
    ),
  );
}
function
mymodule_validation_rule_name($value) {
  if (
preg_match('/^[a-z]+$/', $value)) {
    return
TRUE;
  }
  else {
    return
FALSE;
  }
}
/**
* Rule example with params
*/
function mymodule_validation_other_rule($value, $params) {
  if (
$value != $params[0]) {
    return
TRUE;
  }
  else {
    return
FALSE;
  }
}
function
mymodule_validation_filter_name($value) {
  return
preg_replace('/\d+/', '', $value);
}
?>

And now you can use on your Form API.

<?php
//...
$form['myfield'] = array(
 
'#type' => 'textfield',
 
'#title' => 'Email',
 
'#required' => TRUE,
 
'#rules' => array('rule_name'),
 
'#filters' => array('trim', 'filter_name')
);
// Custom rule with parameters. This field should have "brazil" as value to be valid.
$form['otherfield'] = array(
 
'#type' => 'textfield',
 
'#title' => 'Email',
 
'#required' => TRUE,
 
'#rules' => array('other_rule[brazil]'),
 
'#filters' => array('trim', 'lowercase')
);
//...
?>

Modules using FAPI Validation

Why not Validation API ?

The Validation API module is focused on Drupal end users and not module developers. There is no documentation so I guess that you can only set validations on admin interface.

So FAPI Validation come to give you, developer, more facility on your development process.

Project Information

Downloads