Home
Open Source Works

The ExtraValidate API

To use the ExtraValidate module, special form elements must be used. If callbacks are used, then these need to be provided.

Form Elements

Any form elements can be validated by ExtraValidate. However, since Drupal does such a good job of this for all but textual elements, the 'textfield' and 'textarea' elements are most likely to be used.

The special tag '#extra_validate' is used to instruct the module on how to validate the field. The value must be an array specifying the type of validation to perform.

The 'type' key can be used to specify either 'integer' or 'float', for whole numbers and decmials respectively. Optionally, 'max' and 'min' keys can be used to limit the allowed values.

Alternatively, to use a callback, the key 'callback function' can be used. It's value must be the name of a function that will validate the form element. If the function is in a different module, then the 'callback module' can be used to specify it (in which case, it will be called with module_invoke() rather than directly).

Some examples:

'#extra_validate' => array('type' => 'integer', 'min' => 0);
'#extra_validate' => array('type' => 'float');
'#extra_validate' => array('callback module' => 'mymodule', 'callback function' => 'validator');

Callback Functions

Callback functions are passed three parameters:

  • $form - The form element being validated. This is an array, containing keys such as '#title', '#description' etc.
  • $tree - an array defining the location in the form tree for this element. Most forms use a "flattened" hierarchy, in which case the tree will contain a single element (which is the programatic name of the form element). If #tree has been set to true in the form, then the array will be a list representing the hierarchy, with the last entry being the form element's name.
  • $value - the value of this form element, as provided by the user. It is this that requires checking.

Thus, the prototype for callback functions are:

<?php
function example_validation_callback($form, $tree, $value) {
  ...
}
?>