Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Drupal form validation
PHP
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README.mdown
valiform.inc
valiform.info
valiform.module

README.mdown

Valiform - Form validation API

Valiform provides a unified API for modules to supply validation methods for use in Drupal forms. It extends Drupal's own form API.

Valiform makes it easy to reuse form validation code between different sites.

Usage

Providing validation callbacks

To provide own validation callbacks in your modules you need to implement the hook_valiform_validations() and return an associative array that defines your callbacks.

function valiform_valiform_validations() {
  return array(
    'email' => array(
      '#file'     => 'valiform.inc',
      '#callback' => 'valiform_validate_email',
    ),
    'url' => array(
      '#file'     => 'valiform.inc',
      '#callback' => 'valiform_validate_url',
    ),
  );
}

The key is what you use in your forms to associate a field with a specific callback.

#file is relative to the module file implementing the hook. Can be omitted if callback is in the same file as the hook.

Writing a validation callback

The function signature for validation callbacks are the same as the callback specified with #element_validate in Drupal's form API. As it's first argument it takes an array defining the current element, as it second argument it takes a reference to the $form_state array.

If the validation fails, return a (translated) string describing the error, if no error is encountered you don't need to return anything, but returning TRUE looks better! ;)

function valiform_validate_email($element, &$form_state) {
  if (!is_valid_email($form_state['values]['email'])) {
    return t('The email you provided is invalid!');
  } else {
    return TRUE;
  }
}

Making the validation run

Defining validation rules for elements is as simple as setting valiform_validate as the #element_validate callback and setting #validation to an array containing the names of the validation callbacks you want to run for this element.

Notice that rules can also be weighted.

$form['fadder']['sp'] = array(
  '#type'         => 'textfield',
  '#title'        => 'email',
  '#element_validate' => array('valiform_validate'),
  '#validation' => array(
    'email' => array('#weight' => 7),
  ),
);

There is also an option to supply callbacks with arguments by setting #arguments to an array containing the arguments you want to get passed to the callback. (see http://php.net/call\_user\_func_array)

That's all!

Please report any bugs/issues that you find! =)

Something went wrong with that request. Please try again.