|
|
Forms Extension Framework Documentation03/16/2001
In my previous column, I explained that I'll use form processing as a platform for introducing a few new JavaScript concepts. So in that spirit, this week I'm going to provide you with the nuts and bolts of the Forms Extension Framework, which I'll be referring to as ForX. This is not a regular column in the sense that I'll focus on how to perform a specific task using JavaScript -- those columns are to follow in the future. This is, instead, a reference document designed to help you understand the templates I've included and, ultimately, to help you write your own ForX scripts. Along those lines, I have a few suggestions. First, if you haven't read my two previous articles, Yajc -- Yet Another JavaScript Column and Working With Forms: An Introduction, do so now. Then you might want to look at my first library example, Forms Extension Framework (ForX). After that, breeze through this document once to become familiar with its content. Finally, I would then save this article and refer to it as you are playing with one of the ForX examples. If you follow my suggested path, I think that you'll find yourself coming up to speed quickly with this exciting method of JavaScript form processing. Now, on to the ForX elements themselves. Overview of the Forms Extension FrameworkForX is a set of attributes designed to extend existing form functionality with desirable features such as validation, content types, and groups. ForX is based on two principles:
With ForX, you can specify the elements that must be completed before a form can be submitted. To define these requirements, you use A "group" makes it easier to handle elements that are not associated by default, such as radio buttons. With a conditional requirement you can have a group that's dependent on another element's value or on a literal value. A form implementing ForX attributes must have a unique To make sure a user completed the form correctly, you can use an option to enable a warning after the user tried to send a valid form (which doesn't necessarily mean that the data entered is correct). This element is shown only once (if the form remains valid after reviewing). This behavior is managed by Required elements and non-valid elements are marked with a special element, which is defined in the same way as the warning element above. You are generally free to fill the template elements with any content you want; you only need to have the correct ID and class. The template file contains everything you need to get started. (Use right-click to save, or view source -- the unaltered template file will just view as a blank page!) After that, you should examine the example page for some working demos. NamespaceForX uses its own namespace to avoid conflicts with other elements. The prefix for the namespace is defined as
Every element that is referenced by another ForX element needs to have its own unique ValidationRequirements definitionTo mark any element as being "required" for the form to process, the
where The You can use a literal value, or the name of another element on the same form, on the right side of the condition. A literal value is indicated by single or double quotation marks. Checkboxes and radio buttons can use the keyword
An element is required only when the condition evaluates All required elements need to be specifically marked as defined in the template. A template needs to have class
As long as you meet these requirements, you are free to use whatever you want as your marker. The engine copies this template to all elements and group containers that are required, but not valid. Content typesElements that allow the user to enter any type of text should be checked to contain a special class of content. For this purpose, ForX defines a set of predefined content types. The content type for an element is defined within the Predefined content types:
If no content type is specified for a required element, it's assumed to have the Regular expressionsIn those cases where a predefined set of content types is not good enough, a regular expression is the only solution. To use a regular expression, it has to be put into the GroupingGroups are probably one of the most desirable features. Just imagine the following situation: You need a form for an online store that allows customers to pay via invoice or credit card. For the latter, you need additional data such as the number and expiration date for the card. Previously, "in the old days," you'd write a custom function that forked after it detected the customer's selected payment method. With ForX, you can easily group required elements and make them dependent on other elements. To create such a group you need two things: a container element and a set of member elements. The container element is needed to place the marker element into, which reminds the user to fill out required fields. If a group is not valid, the marker template is copied and shown in the group container element. The container element is identified by the The container element also holds the To highlight all members of a group, you can set the group container's To define the number of elements that must be valid to make the entire group valid, you use the
To add elements to a group you use the It's not possible to retrieve the value of a group or to nest groups. See the example pages for working demos. Claus Augusti is O'Reilly Network's JavaScript editor. Read more Essential JavaScript columns. Return to the JavaScript and CSS DevCenter. |
|
|
|
|
||||||||