<mx:VBoxxmlns:mx="http://www.adobe.com/2006/mxml" width="600" height="400" xmlns:cookbook="oreilly.cookbook.*" creationComplete="init()"> <mx:Script> <![CDATA[ import mx.events.ValidationResultEvent; import mx.validators.Validator; import mx.controls.Alert; [Bindable] privatevar validatorArr:Array; //建立一个包含所有validator的数组, 我们将会以一个方法对其进行检查 privatefunction init():void { validatorArr = new Array(); //push all the validators into the same array validatorArr.push(rbgValidator); validatorArr.push(toggleValidator); validatorArr.push(comboValidator); } //对所有validator数组中的项目进行验证。如果有任何错误的话, 则会发出一个警告。 privatefunction validateForm():void { // the validate all method will validate all the Validators in an array //passed to the validateAll method var validatorErrorArray:Array = Validator.validateAll(validatorArr);; var isValidForm:Boolean = validatorErrorArray.length == 0; if (!isValidForm) { var err:ValidationResultEvent; var errorMessageArray:Array = []; for each (err in validatorErrorArray) { errorMessageArray.push(err.message); } Alert.show(errorMessageArray.join("/n"),"Invalid form...", Alert.OK); } }