gpt4 book ai didi

javascript - MVC 3 ValidationMessageFor() 没有出现

转载 作者:行者123 更新时间:2023-11-30 13:28:07 25 4
gpt4 key购买 nike

我想以 dd-mmm-yyyy 的形式强制执行日期。我使用以下帖子作为指南:

http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx

问题:ValidationMessageFor 文本将不会显示(在回发和客户端)。有什么建议吗?

更新:

通过包含@Darin-Demintrov 的回答,这个问题中的代码现在可以工作了。请注意,验证消息在选择日期后仍然存在。要解决此问题,必须根据我在下面的回答处理 DatePicker onChange() 事件。

查看模型属性:

    [Required(ErrorMessage = "* required")]
[Display(Name = "Event Date")]
[PpcDate]
public DateTime EventDate { get; set; }

PpcDate ValidationAttribute 类:

public class PpcDateAttribute : ValidationAttribute, IClientValidatable, IMetadataAware
{
/// <summary>
/// Any valid DateTime is fine;
/// the regex verification only happens on the client
/// </summary>
public override bool IsValid(object value)
{
if (value == null || (value is DateTime))
return true;
else
return false;
}

public override string FormatErrorMessage(string name)
{
return string.Format("{0} must be in the form dd-mmm-yyyy", name);
}

#region IClientValidatable Members

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.DisplayName),
ValidationType = "ppcdate"
};
}

#endregion

#region IMetadataAware Members

public void OnMetadataCreated(ModelMetadata metadata)
{
metadata.DataTypeName = "Date";
}

#endregion
}

View :

@Html.EditorFor(model => model.EventDate)
@Html.ValidationMessageFor(model => model.EventDate)

和 javascript 连接:

(function ($) {
// The validator function
$.validator.addMethod('ppcdate', function (value) {
if (!value) {
return true;
} else {
return /^\d{2}-\w{3}-\d{4}$/.test(value);
}

});

// The adapter to support ASP.NET MVC unobtrusive validation
$.validator.unobtrusive.adapters.add('ppcdate', [], function (options) {

// EDIT: Add next line per Darin's answer below.
options.rules['ppcdate'] = true;

if (options.message) {
options.messages['ppcdate'] = options.message;
}
});
} (jQuery));

生成的 html 看起来正确吗?

请注意,data-val-ppcdate 属性按预期显示...

<div class="t-widget t-datepicker">
<div class="t-picker-wrap">
<input autocomplete="off" class="t-input valid" data-val="true" data-val-ppcdate="Event Date must be in the form dd-mmm-yyyy" data-val-required="* required" id="EventDate" name="EventDate" value="28-Sep-2011" type="text">
<span class="t-select">
<span class="t-icon t-icon-calendar" title="Open the calendar">Open the calendar</span>
</span>
</div>
</div>
<span class="field-validation-valid" data-valmsg-for="EventDate" data-valmsg-replace="true"></span>

有什么建议吗?

最佳答案

您忘记注册规则并且您的自定义 ppcdate 验证方法永远不会触发:

$.validator.unobtrusive.adapters.add('ppcdate', [], function (options) {
if (options.message) {
// This is what associates the adapter with the custom validation method
options.rules['ppcdate'] = options.params;
options.messages['ppcdate'] = options.message;
}
});

这是完整的 example .

关于javascript - MVC 3 ValidationMessageFor() 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7789485/

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