Accessibility

Table of Contents

Creating a custom form validator in Flex

Creating the ActionScript class

Create a new project in Flex Builder, and name it CustomValidator (see Figure 1).

The New Flex Project wizard

Figure 1. The New Flex Project wizard

After creating the project, choose File > New > ActionScript Class (see Figure 2). The wizard will ask for the package and class names.

The New ActionScript Class wizard

Figure 2. The New ActionScript Class wizard

If you leave the Package field empty, the wizard will display a warning, "The use of the default package is discouraged." I strongly recommend that you provide a package name. Packages enable you to organize your code in hierarchical structures, making it easier to find and use. Many organizations use their website address in reverse to begin the package name; if your company's web address is www.mycompany.com, the package name would be something like com.mycompany.packageName. In this project, you can use the package name MyUtilities.

You'll be using this custom validator to check filename extensions provided by the user in a TextInput control, limiting them to a few web image formats: .jpg, .jpeg, .gif, and .png. You can use FileExtensionValidator for the class name. You'll take advantage of the built-in Flex Validator superclass, which comes with very useful validation properties and methods. By subclassing – or extending – the Validator superclass, you'll have access to all its functionality, plus you can add your own.

In the wizard, click the Browse button next to the Superclass field; then scroll down and choose "Validator - mx.validators". Leave the other fields at their default values and click OK. Flex generates the class file, FileExtensionValidator.as, and places it in the src folder, under the MyUtilities directory (to indicate package hierarchy, much as in Java).

The generated class will look like this:

package MyUtilities
{
      import mx.validators.Validator;
 
      public class FileExtensionValidator extends Validator
      {
            public function FileExtensionValidator()
            {
                  super();
            }
      }
}