flex 动态new组件并添加验证
工作需要,自己写测试代码测试下
?
?<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"><mx:Script><![CDATA[import mx.events.ValidationResultEvent;import mx.validators.Validator;private var formValidator:FormValidator=new FormValidator();private function init():void{formValidator.addValidators(validators);formValidator.failedCallBack=function():void{submit.enabled=false;};formValidator.passedCallBack=function():void{submit.enabled=true;};}public function test():void{var btn:Button = new Button();var text:TextInput = new TextInput();text.restrict="0-9";var validate1:Validator = new Validator();validate1.source=text;validate1.property="text";validate1.requiredFieldError="该选项不能为空!";this.panel.addChild(text);var text2:TextInput = new TextInput();text2.id ="textid";var validate:Validator = new Validator();validate.source=text2;//validate.addEventListener(ValidationResultEvent.VALID,handleValid);//validate.addEventListener(ValidationResultEvent.INVALID,handleValid);validate.property="text";validate.requiredFieldError="该选项不能为空!";this.panel.addChild(text2);formValidator.addValidator(validate);} // Event listener for the valid and invalid events. private function handleValid(eventObj:ValidationResultEvent):void { if(eventObj.type==ValidationResultEvent.VALID) // Enable Submit button. submit.enabled = true; else submit.enabled = false; }]]></mx:Script><mx:Panel width="400" height="300" id="panel"><mx:Form><mx:FormItem label="地址" required="true" ><mx:TextInput id="address" /></mx:FormItem><mx:FormItem label="电话" required="true" ><mx:TextInput id="phone" /></mx:FormItem><mx:FormItem label="顺序" ><mx:TextInput id="seq" restrict="0-9" maxChars="15"/></mx:FormItem></mx:Form><mx:ControlBar horizontalAlign="center"><mx:Button id="submit" label="提交" enabled="false"/><mx:Button id="close" label="关闭" /><mx:Button label="增加" click="test()"/></mx:ControlBar></mx:Panel><mx:Array id="validators"><mx:Validator required="true" requiredFieldError="地址不能为空!" source="{address}" property="text" /><!--<mx:Validator required="true" requiredFieldError="电话不能为空!" source="{phone}" property="text" />--></mx:Array><!--<mx:NumberValidator id="seq_" source="{seq}" property="text" domain="int" minValue="1" integerError="顺序必须是整数!" invalidCharError='输入了非数字字符!' trigger="{submit}" triggerEvent="click"/>--><mx:Style> .errorTip { fontSize: 12; }</mx:Style></mx:Application>? 1 楼 梦心缘 2011-12-02 这样的做法有一个问题,如果你删除了动态创建的文本框text,可是你的验证是无法通过的,虽然页面上显示已经没有了text这个文本框,可validate1.source=text; 这句还绑定着验证呢?不知博主是怎么解决的?