Advanced data validation

In the Webform Validation module, we have a powerful ally indeed! The basic validation functions that are built into Webform are immensely augmented by the range of possibilities exposed by Webform Validation. The module enables us to specify additional validation rules through a point-and-click interface and, in certain cases, to specify a custom error message for a particular rule.

Getting ready

Let us head on over to the project home page to get the module installed on our site: http://drupal.org/project/webform_validation.

How to do it...

Let's discover how we may utilize the Webform Validation module to extend the range of validations that we may implement on our Webforms.

  1. After the module has been enabled, we will find that our Webforms tabbed menu has a new tab entitled Form validation that invites us to Add a validation rule as shown in the following screenshot:
    How to do it...
  2. We will quickly add a validation rule that requires our speakers to enter at least fifteen words into the Synopsis text area of our example Webform.
  3. Let's commence by clicking on the Minimum number of words rule type heading.
  4. Enter Synopsis - minimum words as the Rule name, check the Synopsis option and enter 15 as the Minimum number of words. Now we click on the Add rule button to save our new validation rule.
    How to do it...
  5. We can now test our validation rule by viewing our Speaker Registration form and trying to submit a form with a very short description in the Synopsis text area.
  6. Of course, now that we have imposed such a rule, our form users would appreciate it if we were to update the Synopsis component description from Please provide a clear and concise summary of the content of your presentation to something along the lines of Please provide a clear and concise summary of the content of your presentation (minimum 15 words). It can cause great frustration when we don't inform our users what is required from them!

How it works...

Webform Validation presents us with several ready made validation options. The purpose and function of each rule type is explained underneath the respective headings on the Form validation page.

Due to variances in the way different types of components are presented and used, not all validation rules are applicable to all component types. Clearly one cannot enforce a Minimum number of selections required rule on a text field, for example. Thus each rule type description includes a list of pertinent component types which that rule can affect. While we are editing a rule, we can select one or more components against which the validation should run via the checkboxes next to the component names.

Of the validation rule options available to us, Regular expression is perhaps the hardest to come to grips with for the average user, although it is an immensely powerful tool to have. With roots in mathematical set theory, regular expression is a formal language that enables the matching, or recognizing, of patterns of characters within strings. It may be utilized to determine whether user input is correctly formatted, contains required words, has required words immediately following other required words (or phrases), and so on.

For those of us unfamiliar with regular expressions, it can seem akin to arcane magic, but any time spent learning how to utilize this tool will repay itself many times over. The Webform Validation home page recommends a visit to the online Regular Expression Library at http://regexlib.com/ to help us find useful examples of regular expressions in action.

As we build up our collection of additional validation rules we will find these listed in a table at the top of the Form validation page. Each row in the table contains links enabling us to modify or delete our rules after we have created them.

There's more...

Already, our capability to validate user input beyond the standard functionality offered by Webform has been enormously extended. However, that is not all there is to it.

Adding custom validation rules

The developers among us will be glad to know that Webform Validation also makes available a collection of hooks which enables us to extend the module and add our own validation rules. In this way, we are able to produce site-wide Webform Validation rules as defined by our various requirements.

More information on extending this module may be found in the README.txt file in the Webform Validation module folder, or on the project home page. We will also cover an example of custom validation in Chapter 8, Acting on Webform Submissions.

Client-side data validation

The default operation of Drupal is to validate data when it is submitted to the web server. This is called server-side validation, because the validation processes are handled by PHP programming that resides on the web server and can only occur after the data has been sent (that is, submitted) from the web browser to the server.

The Clientside Validation module (http://drupal.org/project/clientside_validation) has a Clientside Webform Validation submodule that integrates nicely with both Webform and Webform Validation to bring our validation rules to the web client (that is, the web browser being used to view our form). The effect this has is that field validation occurs immediately after the user moves away from a field and will display required error messages prior to the form being submitted. When the user returns to the field flagged as being in error and corrects their input, the error message disappears.

This type of validation is also known as Ajax form validation or real-time form validation. Clientside Validation makes use of the jQuery JavaScript library to achieve its web client effects.

See also

  • Adding custom validation in Chapter 8,Acting on Webform Submissions
  • Validating on the client side Chapter 12,Going Out of the Box
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset