【发布时间】:2012-06-04 20:23:07
【问题描述】:
有没有人见过需要单个选定日期等于或大于当前日期的日期验证的 MVC3 数据注释?
如果已经有第三方添加,那也很酷。我已经在使用 DataAnnotationsExtensions 但它没有提供我正在寻找的东西。
似乎没有任何关于此的参考。所以,希望有人在我尝试重新发明轮子并编写我自己的自定义验证器之前已经解决了这个问题。
我尝试过Range,但这需要2个日期,并且两者都必须是字符串格式的常量,例如[Range(typeof(DateTime), "1/1/2011", "1/1/2016")],但这无济于事。并且 DataAnnotationsExtensions Min 验证器只接受 int 和 double
更新已解决
感谢@BuildStarted,这就是我最终得到的结果,它在服务器端运行良好,现在与我的脚本一起在客户端运行
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Web.Models.Validation {
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class DateMustBeEqualOrGreaterThanCurrentDateValidation : ValidationAttribute, IClientValidatable {
private const string DefaultErrorMessage = "Date selected {0} must be on or after today";
public DateMustBeEqualOrGreaterThanCurrentDateValidation()
: base(DefaultErrorMessage) {
}
public override string FormatErrorMessage(string name) {
return string.Format(DefaultErrorMessage, name);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
var dateEntered = (DateTime)value;
if (dateEntered < DateTime.Today) {
var message = FormatErrorMessage(dateEntered.ToShortDateString());
return new ValidationResult(message);
}
return null;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) {
var rule = new ModelClientCustomDateValidationRule(FormatErrorMessage(metadata.DisplayName));
yield return rule;
}
}
public sealed class ModelClientCustomDateValidationRule : ModelClientValidationRule {
public ModelClientCustomDateValidationRule(string errorMessage) {
ErrorMessage = errorMessage;
ValidationType = "datemustbeequalorgreaterthancurrentdate";
}
}
}
在我的模型中
[Required]
[DateMustBeEqualOrGreaterThanCurrentDate]
public DateTime SomeDate { get; set; }
客户端脚本
/// <reference path="jquery-1.7.2.js" />
jQuery.validator.addMethod("datemustbeequalorgreaterthancurrentdate", function (value, element, param) {
var someDate = $("#SomeDate").val();
var today;
var currentDate = new Date();
var year = currentDate.getYear();
var month = currentDate.getMonth() + 1; // added +1 because javascript counts month from 0
var day = currentDate.getDate();
var hours = currentDate.getHours();
var minutes = currentDate.getMinutes();
var seconds = currentDate.getSeconds();
today = month + '/' + day + '/' + year + ' ' + hours + '.' + minutes + '.' + seconds;
if (someDate < today) {
return false;
}
return true;
});
jQuery.validator.unobtrusive.adapters.addBool("datemustbeequalorgreaterthancurrentdate");
【问题讨论】:
-
Server DateTime.Today 和客户端 datetime 可能不同。我认为您应该传递服务器端时间,而不是使用 JavaScript new Date()。
-
另一个问题是您假设客户具有美国日期设置 (mm/dd/yyyy) (今天 = 月 + '/' + 天 + '/' + 年 + ' ' + 小时 + '.' + 分钟 + '.' + 秒;)在其他语言环境中不起作用。
-
很好,但我们不支持美国以外的任何其他地区
-
太糟糕了。这意味着我将无法使用您的网站 :-( 我的桌面上有 en-AU 语言环境,我的笔记本电脑上有 ru-UA 语言环境。
标签: asp.net-mvc-3 datetime data-annotations