我在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;
}
}
}
然后我将其称为:
<vld:BirthDateValidator id="dobValidator" source="{dateOfBirth}"
property="text"
trigger="{dateOfBirth}"
triggerEvent="valueCommit"
/>
它可以工作,但是如果你注意到我有一个用于测试的警报窗口,它会显示4次,这意味着验证器被调用了4次。这是正常的吗?或者我做错了什么?
谢谢,
担
答案 0 :(得分:0)
在实例化和配置其父对象期间,可以多次计算绑定变量dateOfBirth。
每个评估都会发生valueCommit事件。