gpt4 book ai didi

jquery - ASP.NET MVC2 自定义 jQuery 验证 : client -side

转载 作者:行者123 更新时间:2023-12-01 07:35:08 25 4
gpt4 key购买 nike

我想为 2 个日期选择器创建验证规则(startDate 小于 endDate)。

我创建一个验证属性:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class DateCompareAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{0}' is less then '{1}'.";

public DateCompareAttribute(string startDateProperty, string endDateProperty)
: base(_defaultErrorMessage)
{
StartDateProperty = startDateProperty;
EndDateProperty = endDateProperty;
}

public string StartDateProperty { get; private set; }
public string EndDateProperty { get; private set; }


public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, StartDateProperty, EndDateProperty);
}

public override bool IsValid(object value)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
object startValue = properties.Find(StartDateProperty, true).GetValue(value);
object endValue = properties.Find(EndDateProperty, true).GetValue(value);
if (startValue.GetType() == typeof(DateTime?) && endValue.GetType() == typeof(DateTime?))
{
var start = ((DateTime?)startValue);
var end = ((DateTime?)endValue);
return (start.Value < end.Value);
}
return false;
}
}

并将 ti 添加到我的 Dto 中:

[DateCompare("StartDate", "EndDate")]
public class QualificationInput{...}

我创建了一个验证器:

public class DateCompareValidator : DataAnnotationsModelValidator<DateCompareAttribute>
{
string startField;
private string endField;
string _message;

public DateCompareValidator(ModelMetadata metadata, ControllerContext context, DateCompareAttribute attribute)
: base(metadata, context, attribute)
{
startField = attribute.StartDateProperty;
endField = attribute.EndDateProperty;
_message = attribute.ErrorMessage;
}

public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
var rule = new ModelClientValidationRule
{
ErrorMessage = _message,
ValidationType = "dateCompare"
};
rule.ValidationParameters.Add("startField", startField);
rule.ValidationParameters.Add("endField", endField);

return new[] { rule };
}
}

并在 Global.asax.cs 的 Application_Start() 中注册它:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DateCompareAttribute), typeof(DateCompareValidator));

在 MicrosoftMvcJQueryValidation.js 中我进行了以下更改:

switch (thisRule.ValidationType)
{
.....
case "dateCompare":
__MVC_ApplyValidator_DateCompare(rulesObj,
thisRule.ValidationParameters["startField"], thisRule.ValidationParameters["endField"]);
break;
.....
}

function __MVC_ApplyValidator_DateCompare(object, startField, endField) {
object["startField"] = startField;
object["endField"] = endField;
}

jQuery.validator.addMethod("dateCompare", function(value, element, params) {
if ($('#' + params["startField"]).val() < $('#' + params["endField"]).val())
{ return true; }
return false;
}, jQuery.format("Error"));

但它不起作用:(这种类型的规则没有客户端验证(其他类型如必需的工作正常)

我做错了什么?

最佳答案

您不需要更改 MicrosoftMvcJQueryValidation.js。我最近不得不自己解决这个问题,以下内容可能会有所帮助;

http://www.deepcode.co.uk/2010/08/custom-mvc-2-validation-using-jquery.html

关于jquery - ASP.NET MVC2 自定义 jQuery 验证 : client -side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2872382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com