Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
1276 CHAPTER 42 In the following example, you create and invoke a validator programmatically in when a user clicks a Button control: <?xml version="1.0"?> <!-- validators\ValTriggerProg.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ // Import ZipCodeValidator. import mx.validators.ZipCodeValidator; private var v:ZipCodeValidator = new ZipCodeValidator(); private function performValidation():void { v.domain = "US or Canada"; // Set the listener property to the component // used to display validation errors. v.listener=myZip; v.validate(myZip.text); } ]]> </mx:Script> <mx:TextInput id="myZip"/> <mx:Button label="Submit" click="performValidation();"/> </mx:Application> Notice that you are still using an event to trigger the performValidation() function that creates and invokes the validator, but the event itself does not automatically invoke the validator. Any errors in the validator are shown on the associated component, just as if you had triggered the validation directly by using an event. Handling the return value of the validate() method You may want to inspect the return value from the validate() method to perform some action when the validation succeeds or fails. The validate() method returns an event object with a type defined by the Valida- tionResultEvent class. The ValidationResultEvent class defines several properties, including the type property. The type property of the event object contains either ValidationResultEvent.VALID or ValidationResultEvent.INVALID , based on the validation results. You can use this property as part of your validation logic, as the following example shows: <?xml version="1.0"?> <!-- validators\ValTriggerProgProcessReturnResult.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ // Import the ValidationResultEvent class. import mx.events.ValidationResultEvent;