取消Validator的验证

对TextInput输入框进行验证时flex中最基本的一些功能,但是在工作当中遇到了一个问题:如果一个输入框是动态的,它需要根据前面的type进行不同的验证,怎么实现?

当对TextInput框进行了StringValidator的验证后,然后根据Type类型,这个TextInput框现在需要用NumericValidator进行验证,但是却发现不能取消此前的StringValidator的验证,即使你把此validator disable掉。但是它的errorString仍然存在。

后来通过研究源代码发现,你不能手动设置它的errorString为空,那样它的oldString仍然不为空,而这个oldString在将来的验证中仍然会使用并在验证时显示出来。

那么怎么才能取消掉这个Validator呢?

 

 

<?xml version="1.0"?>
<!-- validators\StringExample.mxml -->
<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/mx" xmlns:local="*"> 
	<fx:Script>
		<![CDATA[
			import mx.events.ValidationResultEvent;
			import mx.validators.StringValidator;
			import mx.validators.Validator;
			
			import spark.validators.NumberValidator;
			private var dic:Dictionary = new Dictionary();
			protected function button1_clickHandler(event:MouseEvent):void
			{
				if(userNameInput.errorString && userNameInput.errorString != "")
				{
					return;
				}
				var strValidator:StringValidator = new StringValidator();
				strValidator.minLength = 6;
				strValidator.maxLength = 12;
				strValidator.source = userNameInput;
				strValidator.property = "text";
				strValidator.validate();
				dic[userNameInput] = strValidator;
			}
			
			protected function button2_clickHandler(event:MouseEvent):void
			{
				var resultEvent:ValidationResultEvent = new ValidationResultEvent(ValidationResultEvent.VALID);
				(dic[userNameInput] as Validator).dispatchEvent(resultEvent);
				(dic[userNameInput] as Validator).enabled = false;
				delete dic[userNameInput];
			}
			
			protected function button3_clickHandler(event:MouseEvent):void
			{
				var numValidator:NumberValidator = new NumberValidator();
				numValidator.maxValue = 50;
				numValidator.source = userNameInput;
				numValidator.property = "text";
				numValidator.validate();
				dic[userNameInput] = numValidator;
			}
		]]>
	</fx:Script>
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<s:TextInput id="userNameInput"/>
	<s:Button  label="String Validate" click="button1_clickHandler(event)"/>
	<s:Button label="Cancel Validator" click="button2_clickHandler(event)"/>
	<s:Button label="Numeric Validator" click="button3_clickHandler(event)"/>
</s:Application>
 

 

你可能感兴趣的:(验证,Validator,取消)