我愿以d-mmm-yyyyy的形式强制执行日期。 Im利用以下员额作为指南:
Problem: The ValidationMessageFor text will not display (both on postback and client side). 任何建议?
<>Update:
《刑法》中包含“Darin-Demintrov”的答复,现在该法典正在发挥作用。 请注意,验证电文在抽取日期后一直有效。 为了解决这一问题,我必须按下面的回答处理Change(Change)(日期)事件。
View model property:
[Required(ErrorMessage = "* required")]
[Display(Name = "Event Date")]
[PpcDate]
public DateTime EventDate { get; set; }
<>PpcDate ValidationAttribute par:
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
}
www.un.org/Depts/DGACM/index_spanish.htm 观点:
@Html.EditorFor(model => model.EventDate)
@Html.ValidationMessageFor(model => model.EventDate)
www.un.org/Depts/DGACM/index_spanish.htm j:
(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));
www.un.org/Depts/DGACM/index_spanish.htm 由此得出的“html”肯定是正确的?
Note that the data-val-ppcdate attribute appears as expected...
<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>
任何建议?