tags:

views:

67

answers:

1

Hi,

I created a custom validator as follows in Flex 4 beta 2:

// ActionScript file
package com.lal.validators
{
    import mx.controls.Alert;
    import mx.validators.ValidationResult;
    import mx.validators.Validator;


    public class BirthDateValidator extends Validator {

        // Define Array for the return value of doValidation().
        private var results:Array;
        private var MIN_DATE_LENGTH:int = 8;
        private var MAX_DATE_LENGTH:int = 10;
        private var LENGTH_OF_DATE_ARRAY:int = 3;

        // Constructor.
        public function BirthDateValidator() {
            // 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);

            // Format of Date of Birth
            const DDMMYYYY : String = "DDMMYYYY";

            // Clear results Array.
            results = [];

            // Call base class doValidation().
            results = super.doValidation(value);        
            // Return if there are errors.
            if (results.length > 0)
                return results;

            // Case where user didn't input complete date
            if (value.length < MIN_DATE_LENGTH || value.length > MAX_DATE_LENGTH) {
                trace ("IsDateDDMMYYYYValid wrong date length");
                Alert.show("error with length", "hey");
                results.push(new ValidationResult(true, null, "NaN", 
                    "Please enter a date in the Format DD-MM-YYYY"));
                return results;
            }           

            var arrayOfDate:Array = value.toString().split("-")

            if(arrayOfDate.length != LENGTH_OF_DATE_ARRAY){
                trace ("When Splitting the array into three parts I didn't get 3 as it should be");
                Alert.show("error with length", "hey");
                results.push(new ValidationResult(true, null, "NaN", 
                    "Please enter a date in the Format DD-MM-YYYY"));
                return results;
            }

            // Alert.show("day: " + arrayOfDate[0] + " month: " + arrayOfDate[1] + " year: " + arrayOfDate[2]," The Parsed Date.");

            var currentDate:Date = new Date();
            var date:Date = new Date(parseInt(arrayOfDate[2]), parseInt(arrayOfDate[1])-1, parseInt(arrayOfDate[0]));

            if(currentDate < date){
                trace ("Date of Birth is greater than now!");
                Alert.show("error with date position current:" + currentDate + " the date: " + date, "hey");
                results.push(new ValidationResult(true, null, "NaN", 
                    "Please enter a date that is before today's date"));
                return results; 
            }

            return results;
        }
    }
}

Then I call it as follows:

    <vld:BirthDateValidator id="dobValidator" source="{dateOfBirth}" 
                            property="text"
                            trigger="{dateOfBirth}"
                            triggerEvent="valueCommit"
                            />

it works but if you notice I have an Alert window for testing and it gets displayed 4 times which means the validator is getting called 4 times. Is that normal? or am I doing something wrong?

THanks,

Tam

A: 

The bound variable dateOfBirth can be evaluated several times during the instantiation and configuration of it's parent object.

The valueCommit event will occur for each of those evaluations.

dbasch