Use Array.filter() filter out disabled validator
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()">
<mx:Script>
import mx.events.ValidationResultEvent;
import mx.controls.Alert;
import mx.validators.Validator;
private var myValidators:Array;
private function initApp():void {
myValidators = [valid1, valid2].filter(filterDisabledValidators);
}
private function clickHandler():void {
var errors:Array = Validator.validateAll(myValidators);
if (errors.length == 0) {
Alert.show("No errors.", "SUCCESS");
}
}
private function filterDisabledValidators(element:*, index:int, arr:Array):Boolean {
return element.enabled;
}
</mx:Script>
<mx:StringValidator id="valid1" source="{ti1}" property="text" minLength="4" maxLength="6" />
<mx:StringValidator id="valid2" source="{ti2}" property="text" minLength="4" maxLength="6" enabled="false" />
<mx:TextInput id="ti1" />
<mx:TextInput id="ti2" />
<mx:Button label="validate" click="clickHandler()" />
</mx:Application>
Related examples in the same category