Adobe Flex 3 Help

Example: Creating a simple validator

You can use the StringValidator class to validate that a string is longer than a minimum length and shorter than a maximum length, but you cannot use it to validate the contents of a string. This example creates a simple validator class that determines if a person is more than 18 years old based on their year of birth.

This validator extends the Validator base class, as the following example shows:

package myValidators
{
    import mx.validators.Validator;
    import mx.validators.ValidationResult;


    public class AgeValidator extends Validator {

        // Define Array for the return value of doValidation().
        private var results:Array;

        // Constructor.
        public function AgeValidator() {
            // Call base class constructor. 
            super();
        }
    
        // Define the doValidation() method.
        override protected function doValidation(value:Object):Array {
        
            // Convert value to a Number.
            var inputValue:Number = Number(value);

            // Clear results Array.
            results = [];

            // Call base class doValidation().
            results = super.doValidation(value);        
            // Return if there are errors.
            if (results.length > 0)
                return results;
        
            // Create a variable and initialize it to the current date.
            var currentYear:Date = new Date();
    
            // If input value is not a number, or contains no value, 
            // issue a validation error.
            if (isNaN(inputValue) || !value )
            {
                results.push(new ValidationResult(true, null, "NaN", 
                    "You must enter a year."));
                return results;
            }       

            // If calculated age is less than 18, issue a validation error.
            if ((currentYear.getFullYear() - inputValue) < 18) {
                results.push(new ValidationResult(true, null, "tooYoung", 
                    "You must be 18."));
                return results;
            }
            
            return results;
        }
    }
}

This example first defines a public constructor that calls super() to invoke the constructor of its base class. The base class can perform the check to ensure that data was entered into a required field, if you set the required property of the validator to true.

Notice that the second argument of the constructor for the ValidationResult class is null. You use this argument to specify a subfield, if any, of the object being validated that caused the error. When you are validating a single field, you can omit this argument. For an example that validates multiple fields, see Example: Validating multiple fields.

You can use this validator in your Flex application, as the following example shows:

<?xml version="1.0" ?>
<!-- validators/MainAgeValidator.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:MyComp="myValidators.*">

    <MyComp:AgeValidator id="ageV" 
        required="true" 
        source="{birthYear}" 
        property="text" />
    
    <mx:Form >
        <mx:FormItem label="Enter birth year: ">
            <mx:TextInput id="birthYear"/>
        </mx:FormItem>
        <mx:FormItem label="Enter birth year: ">
            <mx:Button label="Submit"/>
        </mx:FormItem>
    </mx:Form>
    
</mx:Application>

The executing SWF file for the previous example is shown below:

The package statement for your custom validator specifies that you should deploy it in a directory called myValidators. In the previous example, you place it in the subdirectory of the directory that contains your Flex application. Therefore, the namespace definition in your Flex application is xmlns:MyComp="myValidators.*". For more information on deployment, see Component Compilation.