MVC 3.0 - 不显眼的客户端验证不适用于自定义属性
我是 mvc 3.0 和 jquery 的新手。我正在尝试使用自定义的验证属性在客户端和服务器端验证“日期”。它在服务器端工作正常,但无法使其在客户端工作。
我使用 mvc 3.0、jquery、IE 7.0。 在 MVC 3.0 中,我们需要在 global.ascx 中注册任何东西吗?
请让我知道我错在哪里。 TIA。
这是我的代码:
验证属性
public class FutureDateAttribute : ValidationAttribute, IClientValidatable
{
private const string DateFormat = "mm/dd/yyyy";
private const string DefaultErrorMessage = "'{0}' must be a date between {1:d} and current date.";
public DateTime Min { get; set; }
public DateTime Max { get; set; }
public FutureDateAttribute(string min)
: base(DefaultErrorMessage)
{
Min = ParseDate(min);
Max = DateTime.Now;
}
public override bool IsValid(object value)
{
if (value == null || !(value is DateTime))
{ return true; }
DateTime dateValue = (DateTime)value;
return Min <= dateValue && dateValue <= Max;
}
private static DateTime ParseDate(string dateValue)
{
return DateTime.ParseExact(dateValue, DateFormat, System.Globalization.CultureInfo.InvariantCulture);
}
public override string FormatErrorMessage(string name)
{
return String.Format(System.Globalization.CultureInfo.CurrentCulture, ErrorMessageString, name, Min);
}
public class ModelClientValidationFutureDateRule : ModelClientValidationRule
{
public ModelClientValidationFutureDateRule(string errorMessage,
DateTime min)
{
ErrorMessage = errorMessage;
ValidationType = "futuredate";
ValidationParameters["min"] = min.ToString("mm/dd/yyyy");
ValidationParameters["max"] = DateTime.Now.ToString("mm/dd/yyyy");
}
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationFutureDateRule("Error message goes here", this.Min);
yield return rule;
}
jquery
(function ($) {
$.validator.addMethod('futuredate', function (value, element, param) {
if (!value) return false;
var min = $(param.min).val();
var max = $(param.max).val();
if (value < min || value > max) {
return false;
}
return true;
});
$.validator.unobtrusive.adapters.add(
'futuredate', ['min', 'max'],
function (options) {
var params = {
min: options.params.min,
max: options.params.max
};
options.rules['futuredate'] = params;
if (options.message) {
options.messages['futuredate'] = options.message;
}
});
} (jQuery));
参考
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
///reference path="jquery-1.5.1.min.js" />
///reference path="jquery.validate.js" />
///reference path="jquery-ui-1.8.11.js" />
///reference path="jquery.validate.unobtrusive.min.js" />
///reference path="jquery.validate-vsdoc.js" />
模型:
[DisplayName("Assigned Date :")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[Required(ErrorMessage = "Assigned Date is required")]
[DataType(DataType.Date)]
[FutureDate("12/31/1899", ErrorMessage = "'{0}' must be a date between {1:d} and current date.")]
public DateTime? AssignedDate { get; set; }
I am new to mvc 3.0 and jquery. i am trying to validate the 'date' both at client side and server side using customized validation attributes. It is working fine at server side, but am not able to make it work at client side.
am using mvc 3.0, jquery, IE 7.0.
do we need to register any thing in global.ascx in MVC 3.0?
Please let me know where I am wrong.
TIA.
Here is my code:
Validation Attribute
public class FutureDateAttribute : ValidationAttribute, IClientValidatable
{
private const string DateFormat = "mm/dd/yyyy";
private const string DefaultErrorMessage = "'{0}' must be a date between {1:d} and current date.";
public DateTime Min { get; set; }
public DateTime Max { get; set; }
public FutureDateAttribute(string min)
: base(DefaultErrorMessage)
{
Min = ParseDate(min);
Max = DateTime.Now;
}
public override bool IsValid(object value)
{
if (value == null || !(value is DateTime))
{ return true; }
DateTime dateValue = (DateTime)value;
return Min <= dateValue && dateValue <= Max;
}
private static DateTime ParseDate(string dateValue)
{
return DateTime.ParseExact(dateValue, DateFormat, System.Globalization.CultureInfo.InvariantCulture);
}
public override string FormatErrorMessage(string name)
{
return String.Format(System.Globalization.CultureInfo.CurrentCulture, ErrorMessageString, name, Min);
}
public class ModelClientValidationFutureDateRule : ModelClientValidationRule
{
public ModelClientValidationFutureDateRule(string errorMessage,
DateTime min)
{
ErrorMessage = errorMessage;
ValidationType = "futuredate";
ValidationParameters["min"] = min.ToString("mm/dd/yyyy");
ValidationParameters["max"] = DateTime.Now.ToString("mm/dd/yyyy");
}
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationFutureDateRule("Error message goes here", this.Min);
yield return rule;
}
jquery
(function ($) {
$.validator.addMethod('futuredate', function (value, element, param) {
if (!value) return false;
var min = $(param.min).val();
var max = $(param.max).val();
if (value < min || value > max) {
return false;
}
return true;
});
$.validator.unobtrusive.adapters.add(
'futuredate', ['min', 'max'],
function (options) {
var params = {
min: options.params.min,
max: options.params.max
};
options.rules['futuredate'] = params;
if (options.message) {
options.messages['futuredate'] = options.message;
}
});
} (jQuery));
References
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
///reference path="jquery-1.5.1.min.js" />
///reference path="jquery.validate.js" />
///reference path="jquery-ui-1.8.11.js" />
///reference path="jquery.validate.unobtrusive.min.js" />
///reference path="jquery.validate-vsdoc.js" />
Model:
[DisplayName("Assigned Date :")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[Required(ErrorMessage = "Assigned Date is required")]
[DataType(DataType.Date)]
[FutureDate("12/31/1899", ErrorMessage = "'{0}' must be a date between {1:d} and current date.")]
public DateTime? AssignedDate { get; set; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以看到您的代码有几个问题。第一个:
必须是:
因为
mm
表示分钟而不是几个月。同样的评论:
这必须是:
在客户端,您也有一些问题。在您的
futuredate
验证方法中,您似乎正在执行var min = $(param.min).val();
,这会转换为var min = $(' 12/31/1899').val();
这显然没有多大意义。您必须将这些值解析为 javascript Date 实例,然后才能进行比较他们。所以这就是我建议你的:
这是我用于测试用例的验证属性的完整代码:
以及模型:
和控制器:
There are a couple of issues I can see with your code. The first one:
must really be:
because
mm
means minutes not months.Same remark for:
which must be:
On the client side you have a couple of issues as well. In your
futuredate
validation method you seem to be doingvar min = $(param.min).val();
which translates tovar min = $('12/31/1899').val();
which obviously doesn't make much sense. You will have to parse those values into javascript Date instances before being able to compare them.So here's what I would suggest you:
and here's the full code of the validation attribute I have used for my test case:
And the model:
and the controller: