Flex 自定义 Validator

flex 自定义验证器(Validator),先看下 示例(有点大,500多K,请耐心等待加载 ) 好像显示不出来flash了?
直接访问 这里



代码如下:
ValidatorDate.as

package utils
{
	import mx.controls.DateField;
	import mx.validators.ValidationResult;
	import mx.validators.Validator;
	
	/**
	 * 用于验证起始时间必须小于结束时间。<br>
	 * 使用时必须指定 startDateField 与 endDateField,可选属性 errorMessage。<br>
	 * 注意:由于涉及到两个控件,所以对于 required 判断是同时的(必须两个时间同时不为空),可自定义 requiredFieldError 使它看上去"正确"。
	 */
	public class ValidatorDate extends Validator
	{
		public function ValidatorDate()
		{
			super();
		}
		
		private var _startDateField:DateField;

		public function get startDateField():DateField
		{
			return _startDateField;
		}

		public function set startDateField(value:DateField):void
		{
			removeListenerHandler();
			
			_startDateField = value;
			
			addListenerHandler();
		}

		private var _endDateField:DateField;

		public function get endDateField():DateField
		{
			return _endDateField;
		}

		public function set endDateField(value:DateField):void
		{
			removeListenerHandler();
			
			_endDateField = value;
			
			addListenerHandler();
		}
		
		private var _errorMessage:String = "起始时间不能晚于结束时间";

		public function get errorMessage():String
		{
			return _errorMessage;
		}

		public function set errorMessage(value:String):void
		{
			_errorMessage = value;
		}
		
		
		override protected function get actualListeners():Array
		{
			return [_startDateField, _endDateField];
		}
		
		override protected function doValidation(value:Object):Array
		{
			var results:Array = super.doValidation(value);
			
			if (results.length > 0 || ((value == null) && !required))
			{
				return results;
			}
			else
			{
				var start:Date = value.startDate;
				var end:Date = value.endDate;
				if(start.time > end.time)
				{
					var vr:ValidationResult = new ValidationResult(true, "", "startIsAfterEnd", errorMessage);
					results.push(vr);
				}
			}
			
			return results;
		}
		
		override protected function getValueFromSource():Object
		{
			var value:Object = {};
			
			if(_startDateField && _endDateField && _startDateField.selectedDate && _endDateField.selectedDate)
			{
				value.startDate = _startDateField.selectedDate;
				value.endDate = _endDateField.selectedDate;
				
				return value;
			}
			else 
			{
				return null;
			}
		}
		
		
	}
}


示例程序: ValidateSample.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
		 xmlns:s="library://ns.adobe.com/flex/spark" 
		 xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300" xmlns:utils="utils.*">
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
		<utils:ValidatorDate id="vd" startDateField="{startDateField}" endDateField="{endDateField}" 
							 trigger="{submitButton}" triggerEvent="click"
							 requiredFieldError="开始日期与结束日期都不能为空"
							 errorMessage="开始日期不能晚于结束日期"/>
	</fx:Declarations>
	
	<s:Form width="100%" height="100%">
		<s:FormHeading label="验证操作示例"/>
		<s:FormItem label="开始日期">
			<!--这里 formatString 是必须设置的属性,否则 selectedDate 始终为null -->
			<mx:DateField id="startDateField" formatString="YYYY-MM-DD"/>
		</s:FormItem>
		<s:FormItem label="结束日期">
			<mx:DateField id="endDateField" formatString="YYYY-MM-DD"/>
		</s:FormItem>
		<s:FormItem>
			<s:Button id="submitButton" label="提交" click="trace('start date:', startDateField.selectedDate);"/>
		</s:FormItem>
	</s:Form>
</s:Group>


似乎不怎么需要解说,还有问题的话留言吧

你可能感兴趣的:(Flex,Validator,验证器)