The data that a user enters in a user interface might or might not be appropriate for the application. In Flex, you use a validator to ensure the values in the fields of an object meet certain criteria. For example, you can use a validator to ensure that a user enters a valid phone number value in a TextInput control.
Flex includes a set of validators for common types of user input data, such as ZIP codes, phone numbers, and credit cards. Although Flex supplies a number of commonly used validators, your application may require you to create custom validator classes. The mx.validators.Validator class is an ActionScript class that you can extend to add your own validation logic. Your classes can extend the functionality of an existing validator class, or you can implement new functionality in your custom validator class.
The following image shows the class hierarchy for validators:
Your custom validator class must contain an override of the protected Validator.doValidation() method that takes a single argument, value, of type Object, and returns an Array of ValidationResult objects. You return one ValidationResult object for each field that the validator examines and that fails the validation. For fields that pass the validation, you omit the ValidationResult object.
You do not have to create a ValidationResult object for fields that validate successfully. Flex creates those ValidationResult objects for you.
The base Validator class implements the logic to handle required fields by using the required property. When set to true, this property specifies that a missing or empty value in a user-interface control causes a validation error. To disable this verification, set this property to false.
In the doValidation() method of your validator class, you typically call the base class's doValidation() method to perform the verification for a required field. If the user did not enter a value, the base class issues a validation error stating that the field is required.
The remainder of the doValidation() method contains your custom validation logic.
The doValidation() method returns an Array of ValidationResult objects, one for each field that generates a validation error. The ValidationResult class defines several properties that let you record information about any validation failures, including the following:
errorCode
A String that contains an error code. You can define your own error codes for your custom validators.
errorMessage
A String that contains the error message. You can define your own error messages for your custom validators.
isError
A Boolean value that indicates whether or not the result is an error. Set this property to true.
subField
A String that specifies the name of the subfield associated with the ValidationResult object.
In your override of the doValidation() method, you can define an empty Array and populate it with ValidationResult objects as your validator encounters errors.
You use the Validator.validate() method to programmatically invoke a validator from within a Flex application. However, you should never override this method in your custom validator classes. You need to override only the doValidation() method.