Part 82 - Creating custom validation attribute in asp.net mvc
来源:互联网 发布:金山数据恢复手机版 编辑:程序博客网 时间:2024/04/30 11:04
At the moment, any value outside the range of "01/01/2000" and "01/01/2010" for HireDate filed, will raise a validation error.
But, let's say, we want the end date to be today's date instead of the hardcode "01/01/2010" value. To achieve this we would be tempted to use DateTime.Now.ToShortDateString() as shown below.
At this point, if you compile, you will get an error - An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.
To fix this, we can create a custom DateRangeAttribute. Here are the steps
1. Right click on the project name in solution explorer, and add "Common" folder.
2. Right click on the "Common" folder and add a class file with name = DateRangeAttribute.cs
3. Copy and paste the following code in DateRangeAttribute.cs class file.
Let's now look at another example of creating a custom validation attribute. Let's say our business rules have changed, and the HireDate property should allow any valid date that is <= Today's Date. This means, there is no minimum value restriction and the maximum value should be less than or equal to Today's date. To achieve this, let's add another custom validation attribute. Here are the steps
1. Right click on the "Common" folder and add a class file with name = CurrentDateAttribute.cs
2. Copy and paste the following code in CurrentDateAttribute.cs class file.
3. Decorate "HireDate" property with our custom CurrentDateAttribute as shown below.
[Range(typeof(DateTime), "01/01/2000", "01/01/2010")][DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]public DateTime HireDate { get; set; }
But, let's say, we want the end date to be today's date instead of the hardcode "01/01/2010" value. To achieve this we would be tempted to use DateTime.Now.ToShortDateString() as shown below.
[Range(typeof(DateTime), "01/01/2000", DateTime.Now.ToShortDateString())][DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]public DateTime HireDate { get; set; }
At this point, if you compile, you will get an error - An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.
To fix this, we can create a custom DateRangeAttribute. Here are the steps
1. Right click on the project name in solution explorer, and add "Common" folder.
2. Right click on the "Common" folder and add a class file with name = DateRangeAttribute.cs
3. Copy and paste the following code in DateRangeAttribute.cs class file.
using System;using System.ComponentModel.DataAnnotations;namespace MVCDemo.Common{ public class DateRangeAttribute : RangeAttribute { public DateRangeAttribute(string minimumValue) : base(typeof(DateTime), minimumValue, DateTime.Now.ToShortDateString()) { } }}4. Finally decorate "HireDate" property with our custom DateRangeAttribute as shown below. Notice that, we are only passing the minimum date value. Maximum date value will be today's date. Please note, DateRangeAttribute is present in MVCDemo.Common namespace.
[DateRange("01/01/2000")][DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]public DateTime HireDate { get; set; }
Let's now look at another example of creating a custom validation attribute. Let's say our business rules have changed, and the HireDate property should allow any valid date that is <= Today's Date. This means, there is no minimum value restriction and the maximum value should be less than or equal to Today's date. To achieve this, let's add another custom validation attribute. Here are the steps
1. Right click on the "Common" folder and add a class file with name = CurrentDateAttribute.cs
2. Copy and paste the following code in CurrentDateAttribute.cs class file.
using System;using System.ComponentModel.DataAnnotations;namespace MVCDemo.Common{ public class CurrentDateAttribute : ValidationAttribute { public override bool IsValid(object value) { DateTime dateTime = Convert.ToDateTime(value); return dateTime <= DateTime.Now; } }}
3. Decorate "HireDate" property with our custom CurrentDateAttribute as shown below.
[CurrentDate][DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]public DateTime HireDate { get; set; }Please note that the validation error message can be customised using named parameter "ErrorMessage" as shown below.[CurrentDate(ErrorMessage = "Hire Date must be less than or equal to Today's Date")][DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]public DateTime HireDate { get; set; }
0 0
- Part 82 - Creating custom validation attribute in asp.net mvc
- Part 85 - Enable client side validation in asp.net mvc
- Part 88 - Unobtrusive validation in asp.net mvc
- Part 89 - Remote validation in asp.net mvc
- Part 81 - Range attribute in asp.net mvc
- Part 83 - RegularExpression attribute in asp.net mvc
- Part 84 - Compare attribute in asp.net mvc
- Creating Custom Exceptions in .NET
- Creating Custom Classes in LotusScript, part 1
- Part 3 - Using custom view engines with asp.net mvc
- Part 60 - ViewStart in asp.net mvc
- Part 86 - ValidationSummary in asp.net mvc
- Understanding Request Validation in ASP.NET MVC 3
- Authorize attribute and jquery AJAX in asp.net MVC
- Part 71 - childactiononly attribute in mvc
- Part 72 - HandleError attribute in mvc
- Validation expression in ASP.NET
- ASP.NET Validation in Depth
- 鸡尾酒疗法
- 哈欠:微信小程序开发工具的数据,配置,日志等目录在哪儿? 怎么找?
- 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如 2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制
- 基于SuperSocket实现的WebSocket服务器 和Unity中使用Websocket
- idea2016中tomcat配置及运行
- Part 82 - Creating custom validation attribute in asp.net mvc
- SQLserver2008数据表的修改操作
- 数据结构1
- Maven+Nexus+Myeclipse集成
- Android 自定义View教程(一)
- python爬虫入门之爬取贴吧标题
- [JS]删除链表的重复结点2
- Android自定义View教程(二)
- VS2010连接MySQL数据库的平台搭建(C/C++)