博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC中的统一验证机制~终极了(自己的改良版)
阅读量:7165 次
发布时间:2019-06-29

本文共 11604 字,大约阅读时间需要 38 分钟。

本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善

1 namespace Web.Mvc.Extensions  2 {  3   4     #region 验证基类  5     ///   6     /// 通用验证基类  7     ///   8     public abstract class EntityValidationAttribute : ValidationAttribute  9     { 10         #region Constructors 11         public EntityValidationAttribute(MessageType messageId, params object[] args) : 12             base(() => MessageManager.Current.GetMessage(messageId, args)) { } 13         #endregion 14  15         #region Protected Properties 16         protected virtual Regex rLetters { get { return new Regex("[a-zA-Z]{1,}"); } } 17         ///  18         /// 验证数字 19         /// 子类可以根据自己的逻辑去重写 20         ///  21         protected virtual Regex rDigit { get { return new Regex("[0-9]{1,}"); } } 22         ///  23         /// 验证邮编 24         /// 子类可以根据自己的逻辑去重写 25         ///  26         protected virtual Regex rPostNumber { get { return new Regex("^[0-9]{3,14}$"); } } 27         ///  28         /// 验证手机 29         /// 子类可以根据自己的逻辑去重写 30         ///  31         protected virtual Regex rMobile { get { return new Regex(@"^1[3|4|5|8][0-9]\d{8}$"); } } 32         ///  33         /// 验证电话 34         /// 子类可以根据自己的逻辑去重写 35         ///  36         protected virtual Regex rTelePhone { get { return new Regex(@"^[0-9]{2,4}-\d{6,8}$"); } } 37         ///  38         /// 验证传真 39         /// 子类可以根据自己的逻辑去重写 40         ///  41         protected virtual Regex rFex { get { return new Regex(@"/^[0-9]{2,4}-\d{6,8}$"); } } 42         ///  43         /// 验证Email 44         /// 子类可以根据自己的逻辑去重写 45         ///  46         protected virtual Regex rEmail { get { return new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } } 47         #endregion 48  49     } 50     #endregion 51  52     #region 具体验证模块 53     ///  54     /// 为空验证 55     ///  56     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 57     public class RequiredAttribute : EntityValidationAttribute 58     { 59         public bool AllowEmptyStrings { get; set; } 60         public RequiredAttribute(MessageType messageType, params object[] args) : 61             base(messageType, args) 62         { } 63         public override bool IsValid(object value) 64         { 65             return new System.ComponentModel.DataAnnotations.RequiredAttribute { AllowEmptyStrings = this.AllowEmptyStrings }.IsValid(value); 66         } 67     } 68     ///  69     /// 范围验证 70     ///  71     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] 72     public class RangeAttribute : EntityValidationAttribute 73     { 74         private System.ComponentModel.DataAnnotations.RangeAttribute innerRangeAttribute; 75  76         public RangeAttribute(double minimum, double maximum, MessageType messageType, params object[] args) : 77             base(messageType, args) 78         { 79             innerRangeAttribute = new System.ComponentModel.DataAnnotations.RangeAttribute(minimum, maximum); 80         } 81  82         public RangeAttribute(int minimum, int maximum, MessageType messageType, params object[] args) : 83             base(messageType, args) 84         { 85             innerRangeAttribute = new System.ComponentModel.DataAnnotations.RangeAttribute(minimum, maximum); 86         } 87  88         public RangeAttribute(Type type, string minimum, string maximum, MessageType messageType, params object[] args) : 89             base(messageType, args) 90         { 91             innerRangeAttribute = new System.ComponentModel.DataAnnotations.RangeAttribute(type, minimum, maximum); 92         } 93  94         public override bool IsValid(object value) 95         { 96             return innerRangeAttribute.IsValid(value); 97         } 98     } 99 100     /// 101     /// Email验证102     /// 103     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]104     public class EmailAttribute : EntityValidationAttribute105     {106         public EmailAttribute(MessageType messageType, params object[] args) :107             base(messageType, args) { }108         public override bool IsValid(object value)109         {110             if (value == null)111                 return false;112             else113                 return rEmail.IsMatch(value.ToString());114         }115     }116 117     /// 118     /// 数值验证119     /// 120     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]121     public class DigitAttribute : EntityValidationAttribute122     {123         public DigitAttribute(MessageType messageType, params object[] args) :124             base(messageType, args) { }125         public override bool IsValid(object value)126         {127             if (value == null)128                 return false;129             else130                 return rDigit.IsMatch(value.ToString());131         }132 133     }134 135     /// 136     /// 邮编验证137     /// 138     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]139     public class PostNumberAttribute : EntityValidationAttribute140     {141         public PostNumberAttribute(MessageType messageType, params object[] args) :142             base(messageType, args) { }143         public override bool IsValid(object value)144         {145             if (value == null)146                 return false;147             else148                 return rPostNumber.IsMatch(value.ToString());149         }150 151     }152 153     /// 154     /// 手机验证155     /// 156     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]157     public class MobileAttribute : EntityValidationAttribute158     {159         public MobileAttribute(MessageType messageType, params object[] args) :160             base(messageType, args) { }161         public override bool IsValid(object value)162         {163             if (value == null)164                 return false;165             else166                 return rMobile.IsMatch(value.ToString());167         }168     }169 170     /// 171     /// 电话验证172     /// 173     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]174     public class TelePhoneAttribute : EntityValidationAttribute175     {176         public TelePhoneAttribute(MessageType messageType, params object[] args) :177             base(messageType, args) { }178         public override bool IsValid(object value)179         {180             if (value == null)181                 return false;182             else183                 return rTelePhone.IsMatch(value.ToString());184         }185     }186 187     /// 188     /// 传真验证189     /// 190     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]191     public class FexAttribute : EntityValidationAttribute192     {193         public FexAttribute(MessageType messageType, params object[] args) :194             base(messageType, args) { }195         public override bool IsValid(object value)196         {197             if (value == null)198                 return false;199             else200                 return rFex.IsMatch(value.ToString());201         }202     }203     #endregion204 205     #region 验证消息返回类206     /// 207     /// 消息类208     /// 209     public class MessageManager210     {211         static Dictionary
messages = new Dictionary
();212 static MessageManager()213 {214 messages.Add(MessageType.RequiredField, "这个 \"{0}\"是必填的!");215 messages.Add(MessageType.GreaterThan, "这个 \"{0}\" 的值必须大于 \"{1}\"!");216 messages.Add(MessageType.LessThan, "这个 \"{0}\" 的值必须小于 \"{1}\"!");217 messages.Add(MessageType.EmailField, "这个 \"{0}\" 不是有效的Email地址!");218 messages.Add(MessageType.DigitField, "这个 \"{0}\" 不是有效的数字!");219 messages.Add(MessageType.PostNumberField, "这个 \"{0}\" 不是有效的邮编!");220 messages.Add(MessageType.MobileField, "这个 \"{0}\" 不是有效的手机号码!");221 messages.Add(MessageType.TelePhoneField, "这个 \"{0}\" 不是有效的电话号码!");222 messages.Add(MessageType.FexField, "这个 \"{0}\" 不是有效的传真!");223 }224 ///
225 /// 得到验证异常的消息集合226 /// 对外公开227 /// 228 ///
异常消息ID229 ///
消息参数集合230 ///
231 public string GetMessage(MessageType messageType, params object[] args)232 {233 return string.Format(CultureInfo.CurrentCulture, messages[messageType], args);234 }235 ///
236 /// 本类的实例对象237 /// 238 public static MessageManager Current = new MessageManager();239 }240 241 242 243 #endregion244 245 #region 验证类型枚举246 ///
247 /// 验证消息类型248 /// 249 public enum MessageType250 {251 ///
252 /// 为空验证253 /// 254 RequiredField,255 ///
256 /// 大于验证257 /// 258 GreaterThan,259 ///
260 /// 小于验证261 /// 262 LessThan,263 ///
264 /// 邮箱验证265 /// 266 EmailField,267 ///
268 /// 数字验证269 /// 270 DigitField,271 ///
272 /// 邮编验证273 /// 274 PostNumberField,275 ///
276 /// 手机验证277 /// 278 MobileField,279 ///
280 /// 电话验证281 /// 282 TelePhoneField,283 ///
284 /// 传真验证285 /// 286 FexField,287 }288 #endregion289 290 }291 292 完整的实体为:293 294 ///
295 /// 人类实体296 /// 297 public class Person298 {299 ///
300 /// 姓名301 /// 302 [DisplayName("姓名"), Required(MessageType.RequiredField, "Name")]303 public string Name { get; set; }304 305 ///
306 /// 年纪307 /// 308 [DisplayName("年纪"), Range(18, int.MaxValue, MessageType.GreaterThan, "Age", 18)]309 public int Age { get; set; }310 311 ///
312 /// 体重313 /// 314 [DisplayName("体重"), Range(int.MinValue, 160, MessageType.LessThan, "Weight", 160)]315 public double Weight { get; set; }316 317 ///
318 /// 电子邮件319 /// 320 [DisplayName("电子邮件"), Email(MessageType.EmailField, "电子邮件")]321 public string Email { get; set; }322 323 ///
324 /// 手机325 /// 326 [DisplayName("手机"), Mobile(MessageType.MobileField, "Mobile")]327 public string Mobile { get; set; }328 329 ///
330 /// 电话331 /// 332 [DisplayName("电话"), TelePhone(MessageType.TelePhoneField, "TelePhone")]333 public string TelePhone { get; set; }334 335 ///
336 /// 邮编337 /// 338 [DisplayName("邮编"), PostNumber(MessageType.PostNumberField, "PostNumber")]339 public string PostNumber { get; set; }340 341 ///
342 /// 传真343 /// 344 [DisplayName("传真"), Fex(MessageType.FexField, "Fex")]345 public string Fex { get; set; }346 }

 

这就是面向对象的程序设计中对验证的统一,也算是一种抽象了。

 

转载地址:http://rumwm.baihongyu.com/

你可能感兴趣的文章