by Eric J. Duran on May 19, 2011 // Short URL

Module Monday: Field Validation

Write complex validation rules for your fields

Field Validation is a new arrival to the Drupal module directory: its 1st release was just a week ago, and it's designed for Drupal 7's new Field API. What does it do? Field Validation allows you to add simple validation rules to custom Drupal fields, using a simple UI.

The module was inspired by Webform Validation, which adds similar validation to WebForm components. Right now, only basic regular expression validation rules are supported support but the module provides hooks for implementing other validation rules.

One common application is enforcing a certain format for information entered into a text field. Product codes or employee ID numbers are common examples. In the instructions below, we'll assume that the "id_number" field needs to be restricted to '12-abcd' style entries -- two numbers, a dash, then four letters.

First, create a textfield field called id_number and edit the field. Then, add a new Regular Expression validation rule.

Next, fill out the settings form for the field. The regular expression should look like this: ^[0-9]{2}-[a-z]{4}$.

Save the form, and add a new piece of content for the node type you added the field to. When I filled it out, I used the text 'abc-1234' -- as you can see, it displays the error message we specified for the validation rule.

Finally, try it with a valid id. II used '07-eric' -- lucky for me, my name is only four characters! This time around, the node should save without any validation errors.

If you're new to the world of regular expressions, sites like Regular Expressions Info provide libraries of useful regex patterns that can be used for validation. That's about all you can do with the Field Validation

Eric J. Duran

Senior Developer

Comments

Sven Decabooter

Thanks for mentioning

Thanks for bringing this to my attention Eric.

I'm the author of the Webform Validation module. I've been thinking recently of broadening the scope of that module so it could be used in other places. I will contact the author of the Field Validation module and see how we can work together to create a better validation system that works with webforms, fields, etc...

Reply

thePanz

Nice module, thank you for

Nice module, thank you for the review! Reading your post an idea came to my mind: what about an "export as a module" feature of each validation rule? The module could then create a *new* module that contains the data-definition and the given validation function.. thus making the new validation procedure "exportable" easily... only my 2cents :)

Reply

eaton

Blame the editorial dashboard? ;-)

There was a publish-date mixup, and I ended up pushing this article live earlier than expected. We've adjusted its date to the time it actually debuted, and all should be well with the time-space continuum.

We'll be putting up another 'Module Monday' article next week to fill the spot, so my mixup means more featured modules! ;-)

Reply