flex---->自定义验证组件
myValidators.as
package myCompenent { import mx.validators.ValidationResult; import mx.validators.Validator; public class myValidators extends Validator { public function myValidators() { super(); } private var results:Array; //重写验证函数 override protected function doValidation(value:Object):Array { var s:String= value as String; results=[]; results=super.doValidation(value); if(results.length>0) return results; if(s.length>6) { results.push(new ValidationResult(true,"text","StringTooLong","字符长度超过6了")); } return results; } } }
main.mxml
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" xmlns:myCompenent="myCompenent.*"> <fx:Declarations> <myCompenent:myValidators id="mycom" source="{textinput}" property="text"/> </fx:Declarations> <s:Panel x="131" y="54" width="405" height="200" title="自定义验证组件"> <s:TextInput x="133" y="25" id="textinput"/> <s:Label x="133" y="55" width="128" height="29" text="{textinput.errorString}" /> <s:Button x="133" y="92" label="验证" id="btn" click="mycom.validate();"/> </s:Panel> </s:Application>