Code example of implementation.

This module extends the form API to include convenient access to common for submission filters and validation checks. The core form API has no built in validators available to you, nor filters, and all have to be manually placed in a validation function. With this module you simply add the basic filers and/or validators you want to have in your form render area.

You can use the existent filters and rules or create your own. This module is fully extensible using hooks, allowing you to easily add your own and reuse them throughout the site. Abstraction at it's finest!

Current state of Development

Please note that the Drupal 8 version of this module is in early alpha and is under active development. While the functionality to integrate validators and filters is in place and many are working, many do not work and will need to be updated. The module however is at a point where you can easily wrap up any of the broken validators/filters and contribute it back, or simply just create and use your own.

There are currently no plans to continue with work on the Drupal 7 version of this module. Patches with significant testing and work done on them will be considered, in which case please reach out to me regarding them.

IMPORTANT DIFFERENCE BETWEEN D7 AND D8 VERSIONS:
Validation function was previously called "rules" in D7. For those familiar with Drupal, a large contingent of functionality exists with the name Rules, and is semantically confusing. This has all been renamed "validators" for D8 to ensure that the naming conventions are separate. If you are using D7, the examples below use #validators. D7 users will need to use #rules instead.


Available Validators (Formerly in D7: 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,
  '#validators' => 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 you wish to create your own Validators or Filters, you can add them with hook_fapi_validation_validators or 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,
  '#validators' => 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,
  '#validators' => array('other_rule[brazil]'),
  '#filters' => array('trim', 'lowercase')
);

//...
?>

Why not Validation API ?

First of all, it's deprecated now as of 2010. The Validation API module was also focused on providing for UI integration for drupal end users and not module developers. So FAPI Validation come to give you, developer, more facility on your development process.

Supporting organizations: 
Funded time for development during short bench times in between projects.

Project Information

Downloads