Validations

sebagr - March 8, 2009 - 15:05

Validations extends Forms API to add validation procedures, which should minimize the use of custom validation functions.

Currently, it has server-side (i.e. PHP) validations, but will be extended shortly to provide client-side (i.e. JavaScript) validations as well.

Checks included:

  • Type: number, integer, float
  • Min and max number values
  • Min and max selected options (checkboxes and select fields)
  • Min and max string length
  • Valid dates
  • E-mail fields
  • URL fields
  • Regular expressions
  • Custom validation callback functions
  • Require a filed if another field is equal to, different to, greater or lower than a certain value

You can also set your custom global error messages for each validation type in the settings page, but can provide a specific message for any field while building the form, overriding the general settings.

As said, this does not include JavaScript validations for now, but will shortly.

An example of usage follows (note the new '#validations' element):

$form['field'] = array(
       '#type' => 'textfield',
       '#title' => t("A test fied"),
       '#validations' = array(
               'type' => float,
               'min' => 0,
               'max' => 32,
               'callback' => 'callback_function', // arity: ($form, $form_state, $field_name)
               'required-if' => 'other_field!=3', // supported =, ==, !=, <, >, <= and >=. Value can be empty.
       ),
);

I hope this is will serve as a kick-off for a stronger proposal for a validations framework which could possibly include CCK validations.

WARNING: this module is not ready for production use. This is here now to receive feedback, but be warned many features will be changed before it reaches version 1.0

Downloads

Recommended releases

Version Downloads Date Links
6.x-0.1-alpha2 Download (10.47 KB) 2009-Mar-09 Notes

Development releases

Version Downloads Date Links
6.x-0.x-dev Download (12.62 KB) 2009-Mar-11 Notes


 
 

Drupal is a registered trademark of Dries Buytaert.