CustomValidator.validate()

Availability

Flash Player 6 (6.0.79.0).

Edition

Flash MX Professional 2004.

Usage

This method is called automatically; you don't invoke it directly.

Parameters

value The data to be validated; it can be of any type.

Returns

Nothing.

Description

Method; called automatically to validate the data contained by the value parameter. You must implement this method in your subclass of CustomValidator; the default implementation does nothing.

You can use any ActionScript code to examine and validate the data. If the data is not valid, this method should call this.validationError() with an appropriate message. You can call this.validationError() more than once if there are several validation problems with the data.

Since validate() might be called repeatedly, avoid adding code that takes a long time to complete. Your implementation of this method should only check for validity, and then report any errors using CustomValidator.validationError(). Similarly, your implementation should not take any action as a result of the validation test, such as alerting the end user. Instead, create event listeners for valid and invalid events and alert the end user from those event listeners (see the example below).

Example

The following procedure demonstrates how to create and use a custom validator class. The validate() method of the CustomValidator class OddNumbersOnly.as determines any value that is not an odd number to be invalid. The validation occurs whenever a change occurs in the value of a NumericStepper component, which is bound to the text property of a Label component.

To create and use a custom validator class:

  1. Select File > New and choose ActionScript File.
  2. Add the following code to the file:
    class OddNumbersOnly extends mx.data.binding.CustomValidator
    {
        public function validate(value) {
            // make sure the value is of type Number
            var n = Number(value);
            if (String(n) == "NaN") {
                this.validationError("'" + value + "' is not a number.");
                return;
            }
            // make sure the number is odd
            if (n % 2 == 0) {
                this.validationError("'" + value + "' is not an odd number.");
                return;
            }
            // data is OK, no need to do anything, just return
        }
    }
    
  3. Save the file as OddNumbersOnly.as.

    NOTE

     

    The name of the AS file must match the name of the class.

  4. Select File > New and choose Flash File (ActionScript 2.0).
  5. Open the Components panel.
  6. Drag a NumericStepper component from the Components panel to the Stage and name it stepper.
  7. Drag a Label component to the Stage and name it textLabel.
  8. Drag a TextArea component to the Stage and name it status.
  9. Select the NumericStepper component, and open the Component inspector.
  10. Select the Bindings tab in the Component inspector, and click the Add Binding (+) button.
  11. Select the Value property (the only one) in the Add Bindings dialog box, and click OK.
  12. Double-click the Bound To field to open the Bound To dialog box.
  13. In the Bound To dialog box, select the Label component in the Component Path pane and its text property in the Schema Location pane. Click OK.
  14. Select the Label component on the Stage, and click the Schema tab in the Component inspector.
  15. In the Schema Attributes pane, select Custom from the Data Type pop-up menu.
  16. Double-click the Validation Options field in the Schema Attributes pane to open the Custom Validation Settings dialog box.
  17. In the ActionScript Class text box, enter OddNumbersOnly, which is the name of the ActionScript class you created previously. Click OK.
  18. Open the Timeline and select the first frame on Layer 1.
  19. Open the Actions panel.
  20. Add the following code to the Actions panel:
    function dataIsInvalid(evt) {
        if (evt.property == "text") {
            status.text = evt.messages;
        }
    }
    
    function dataIsValid(evt) {
        if (evt.property == "text") {
            status.text = "OK";
        }
    }
    
    textLabel.addEventListener("valid", dataIsValid);
    textLabel.addEventListener("invalid", dataIsInvalid);
    
  21. Save the FLA file as OddOnly.fla to the same folder that contains OddNumbersOnly.as.
  22. Test the SWF file (Control > Test Movie).

    Click the arrows on the NumericStepper component to change its value. Notice the message that appears in the TextArea component when you choose even and odd numbers.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00002676.html