论坛首页 编程语言技术论坛

Flex 之联合验证

浏览 2110 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-06   最后修改:2009-12-09

     在数据库编辑模块的代码中通常在增加一条记录时,都会碰到数据验证的问题,有时候需要多个字段的验证,才能确保符合存储条件,在网上查看了一些联合验证的例子,并结合自己的实际情况写了一个联合验证的功能,基本上分为两个类,一个是MultipleValidaotr ,它继承自 EventDisaptcher,其中含有一个private属性的Array用来存放多个验证类Vlidator。还有个继承自Textinput的ValidatorTextinput ,在它的类中加入了一个Validator。其目标指向ValodatorTextinput本身,这个输入框就添加了验证非空功能。一旦某个验证框通过,则将其加入MultipleValidaotr,并标识为验证为通过,这样等到所有的验证框都通过验证之后来触发事件。在主体函数里写验证通过和不通过的处理函数来触发验证是否通过的执行代码。

 

具体代码如下:

1.带有非空验证的TextInput组件:

public class ValidatorTextInput extends TextInput
	{

		
		public function ValidatorTextInput()
		{
			super();
			this.Validate.source=this;
			this.Validate.property="text";
			this.Validate.required=true;
			this.Validate.requiredFieldError="不能为空";
		}
		
		[Bindable]
		private var _validate:Validator=new Validator();
		
		public function set Validate(_validate:Validator):void
		{
			this._validate=_validate;
		}
		
		public function get Validate():Validator
		{
			return this._validate;
		}

	}

 2.联合验证类MultipleValidaotr:

[Bindable]
	public class MultipleValidator extends EventDispatcher
	{
        private var _passedCallBack : Function;

        private var _failedCallBack : Function;

        private var validators : ArrayCollection = new ArrayCollection();

        private var voteMap : Object = new Object();


        public function MultipleValidator( _validatorInitializedValue : Boolean = false ) {
        	super();
        	validators.removeAll();
        }
        
        private function onChange(event:Event):void{
        	dispatchEvent(new Event(Event.CHANGE));
        }

        public function addValidator( validator : Validator) : void {
        	if(!validators.contains(validator)){
        			validator.trigger=this;
        			validator.triggerEvent=Event.CHANGE;
        			validator.source.addEventListener(FlexEvent.VALUE_COMMIT,onChange);
        			validator.source.addEventListener(Event.CHANGE,onChange);
        		
        			validator.addEventListener(ValidationResultEvent.INVALID,function(event:ValidationResultEvent):void{
        				voteMap[event.target.source]=false;
        				_failedCallBack();
        			});
        			validator.addEventListener(ValidationResultEvent.VALID,function(event:ValidationResultEvent):void{
        				voteMap[event.target.source]=true;        				
			        	refresh();
        			});
        			validators.addItem(validator);
        	}
        }

        public function addValidators( _validators : Array ) : void {
        	if(_validators==null){
        		return;
        	}
        	for each ( var validator : Validator in  _validators){
        		addValidator(validator);
            }
            dispatchEvent(new Event(Event.CHANGE));//刚进入界面就进行一次验证,使用户知道哪些必需填写
        }

        public function removeValidator(validator:Validator):void{
        	delete voteMap[validator.source];
        	if(validators.contains(validator)){
        		validator.source.removeEventListener(FlexEvent.VALUE_COMMIT,onChange);
        		validator.source.removeEventListener(Event.CHANGE,onChange);
        		validators.removeItemAt(validators.getItemIndex(validator));
        	}
        	refresh();
        }

        public function set passedCallBack( _passedCallBack : Function ) : void {
        	this._passedCallBack = _passedCallBack;
        }

        public function set failedCallBack( _failedCallBack : Function ) : void {
        	this._failedCallBack = _failedCallBack;
        }

        public function refresh():void{
        	for ( var index : String in voteMap ) {
					if ( voteMap[index] == false )
					{
						_failedCallBack();
						return;
					}
				}
				_passedCallBack();
        }
	}

 3.调用验证功能的代码:

public function AddMultipleValidator(e:FlexEvent):void
		{
			multiplevalidator.failedCallBack=function():void{
					btnOK.enabled=false;
				};
			multiplevalidator.passedCallBack=function():void{
					btnOK.enabled=true;
				};				
			multiplevalidator.addValidators(ValidateInput(this));			
		}
		
        
        private var multiplevalidator:MultipleValidator=new MultipleValidator(false);
        
        public function ValidateInput(container:Container):Array
        {   
        	var validatorArray:Array=[];
        	var childs:Array=container.getChildren();
        	for each(var child:UIComponent in childs)
        	{
        		if(child is ValidatorTextInput)
        		{
        		   	validatorArray.push((child as ValidatorTextInput).Validate);
        		}
        		else if(child is ValidatorDateField)
        		{
        			validatorArray.push((child as ValidatorDateField).Validate);
        		}
        		   
        	}
        	
        	return validatorArray;
        }

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics