前些日子从研究所临时调回公司,帮忙做另外一个项目的控件验证工作,其实内容非常的简单,就是将用户即将提交至服务器的数据先做一个本地验证,以达到减少服务器压力、提高用户体验的目的。
附上一张图片 这是官方给的一个Demo,各种例子和示例代码,尽管全是英文的,但是对于我这个连2级都没过的人来说也没啥难度,所以不难看出官方的Demo真的是做得很用心。 该控件的大体使用方法是这样的: 1、声明验证控件(DxValidationProvider),当然一般都是直接拖的; 2、声明规则,由于不同的控件的适用规则不同,所以肯定会声明许多规则; 3、规则与控件的绑定; 4、DxValidationProvider执行验证; 起初的时候,并没有发觉什么难度,但是到了后面测试的时候就发现问题了。比如在同一个界面下,存在选择的情况,则应该只验证被选中的控件,未选中的控件不论输入值正确与否都不应该验证,但是根据上面提及的顺序,会发现根本就没有撤销绑定的操作,于是博主找了很久也没有找到撤销绑定的方法, 接着,并尝试了动态创建DxValidationProvider控件,大体逻辑和上面一样,区别就是DxValidationProvider控件为动态生成,作为局部变量理应存在生命周期,即第二次执行同样方法的时候上一次应该被回收了,但是很奇怪的事情又发生了,动态创建没有问题,问题是上一次的结果还在。 博主非常蛋疼啊,偶然间调用了DxValidationProvider控件的Dispose()方法后,发现DxValidationProvider实例化的对象还是没有被回收!只是清空了他的一些数据而已,博主瞬间凌乱了。加上声明规则也是一个复杂的过程,于是楼主最后封装了几个类,在这里和大家分享分享。 class="code_img_closed" src="/Upload/Images/2013102422/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('eeb15035-6df4-450a-a5fc-9be4dcc594e5',event)" src="/Upload/Images/2013102422/2B1B950FA3DF188F.gif" alt="" />public class CustomValidation { private DXValidationProvider provider; /// <summary> /// 条件列表 /// </summary> public List<ControlRule> RuleList { get; set; } public CustomValidation() { provider = new DXValidationProvider(); } /// <summary> /// 执行验证 /// </summary> /// <returns></returns> public bool Validate() { bool flag = true; if (provider != null) { provider.Dispose(); } if (RuleList != null) { foreach (ControlRule item in RuleList) { provider.SetIconAlignment(item.control, ErrorIconAlignment.MiddleRight); provider.SetValidationRule(item.control, item.rule); //if (!provider.Validate() && flag) //{ // flag = false; //} } flag = provider.Validate(); } else { flag = false; } return flag; } }自定义验证类
public class CustomRuleByRegex : ValidationRule { string regex; bool isnotblank;//是否为空 /// <summary> /// 是否为数字 /// </summary> public static string strIsNumeric = "^[\\+\\-]?[0-9]*\\.?[0-9]+$"; /// <summary> /// 电话号码 /// </summary> public static string strPhone = @"(^(\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$|(1(([35][0-9])|(47)|[8][01236789]))\d{8}$"; /// <summary> /// 使用正则表达式验证 /// </summary> /// <param name="regex">正则表达式</param> /// <param name="isnotblank">是否允许为空</param> public CustomRuleByRegex(string regex, bool isnotblank) { this.regex = regex; this.isnotblank = isnotblank; } public override bool Validate(Control control, object value) { bool flag; try { if (value == null || value.ToString().Trim() == string.Empty) { if (isnotblank) { flag = true; } else { flag = false; this.ErrorText = "该值不允许为空!"; } } else { flag = Regex.IsMatch((string)value, regex); } } catch (Exception ex) { this.ErrorText = ex.Message; flag = false; } return flag; } }自定义规则类
public class ControlRule { public Control control; public ValidationRule rule; public ControlRule(Control control, ValidationRule rule) { this.control = control; this.rule = rule; } /// <summary> /// 判断控件是否为空 /// </summary> /// <returns></returns> public static ConditionValidationRule NotEmpty() { ConditionValidationRule rule = new ConditionValidationRule(); rule.ConditionOperator = ConditionOperator.IsNotBlank; rule.ErrorText = "该值不允许为空!"; return rule; } /// <summary> /// 判断是否大于等于某个数 /// </summary> /// <param name="number">数值</param> public static ConditionValidationRule NoLessNumber(decimal number) { ConditionValidationRule rule = new ConditionValidationRule(); rule.ConditionOperator = ConditionOperator.GreaterOrEqual; rule.ErrorText = string.Format("该值不能小于{0}!", number); rule.Value1 = number; return rule; } /// <summary> /// 判断是否大于某个数 /// </summary> /// <param name="number">数值</param> public static ConditionValidationRule GreaterNumber(decimal number) { ConditionValidationRule rule = new ConditionValidationRule(); rule.ConditionOperator = ConditionOperator.Greater; rule.ErrorText = string.Format("该值必须大于{0}!", number); rule.Value1 = number; return rule; } /// <summary> /// 判断是否在指定数(包含)之间 /// </summary> /// <param name="small">小数</param> /// <param name="big">大数</param> /// <returns></returns> public static ConditionValidationRule BetweenNumbers(decimal small, decimal big) { ConditionValidationRule rule = new ConditionValidationRule(); rule.ConditionOperator = ConditionOperator.Between; rule.ErrorText = string.Format("该值不能小于{0}且不能大于{1}!", small, big); rule.Value1 = small; rule.Value2 = big; return rule; } /// <summary> /// 判断结束时间是否不小于指定时间 /// </summary> /// <param name="startTime">指定时间</param> public static ConditionValidationRule NoLessDate(DateTime startTime) { ConditionValidationRule rule = new ConditionValidationRule(); rule.ErrorText = string.Format("结束时间不能小于开始时间!"); rule.ConditionOperator = ConditionOperator.GreaterOrEqual; rule.Value1 = startTime; return rule; } /// <summary> /// 调用正则表达式判断输入格式 /// </summary> /// <param name="strRex">正则表达式</param> /// <param name="isnotblank">是否允许为空</param> public static CustomRuleByRegex AddValueRex(string strRex, bool isnotblank) { CustomRuleByRegex rule = new CustomRuleByRegex(strRex, isnotblank); rule.ErrorText = "输入格式不正确,请重新输入!"; return rule; } }控件规则类
private CustomValidation myvalidation = new CustomValidation(); /// <summary> /// 获取条件列表 /// </summary> /// <returns></returns> private List<ControlRule> GetRuleList() { List<ControlRule> rulelist = new List<ControlRule>(); //起始时间 rulelist.Add(new ControlRule(datS_Time, ControlRule.NotEmpty())); rulelist.Add(new ControlRule(datE_Time, ControlRule.NoLessDate(Convert.ToDateTime(datS_Time.EditValue)))); //高级 if (layoutControlItem2.Visibility == LayoutVisibility.Always) { if (cSCMoreThan.Checked) { rulelist.Add(new ControlRule(spin_TalkingLongGreater, ControlRule.NoLessNumber(0))); } } return rulelist; } //验证 private bool OperateValidate() { bool flag = true; //输入参数基本验证 myvalidation.RuleList = GetRuleList(); if (!myvalidation.Validate()) { flag = false; } string msg; if (!ValueValidate(out msg)) { XtraMessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); flag = false; } return flag; }使用范例