2010-02-28 3 views
0

J'ai créé un validateur personnalisé comme suit dans Flex 4 beta 2:pourquoi validateur est appelé plusieurs fois avec 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; 
     } 
    } 
} 

Alors je l'appelle comme suit:

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

il fonctionne mais si vous remarquez que j'ai une fenêtre d'alerte pour le test et qu'il est affiché 4 fois ce qui signifie que le validateur est appelé 4 fois. Est-ce normal? ou est-ce que je fais quelque chose de mal?

, THanks

Tam

Répondre

0

La dateOfBirth variable liée peuvent être évaluées à plusieurs reprises lors de l'instanciation et la configuration de c'est l'objet parent.

L'événement valueCommit se produira pour chacune de ces évaluations.

Questions connexes