11.12. Assembling an Advanced Form

Problem

You want to put together a form to which you can add advanced features such as one-step submission, validation, and multipage forms.

Solution

Create a custom Form class.

Discussion

In HTML, forms are created within the structure of a FORM element. Because HTML forms provide a container into which all the form elements can be added, it is possible to add more advanced functionality to them. ActionScript does not provide an analogous structure, however. Therefore, Flash offers nothing that inherently holds multiple elements on a page together into a true form. If you want to add advanced features to your forms, you must create a Form class to act as the container for all the form elements.

The Form class’s primary function is to keep track of the otherwise disparate elements (text fields, list boxes, etc.) that you want to be part of a single form. To accomplish this, your Form class needs to have an array property to which elements can be added. In the example code, this array property is named formElements. Next, the Form class also needs a method, addElement( ), by which elements can be added to forms.

Here is the basic implementation of our Form class:

// Define the Form constructor as a global class, as with other UI components.
_global.Form = function (  ) {

  // Initialize the formElements array to store references to all the elements.
  this.formElements = new Array(  );
};

// The addElement(  ) method adds an element to the formElements array. ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.