Relates to: ASP.NET MVC 3 client-side validation with parameters
I built a custom validation attribute allowing me to check relative dates (ex. model value less than or equal to Today).
The validator correctly implements the GetClientValidationRules
method and the HTML5 emitted in the view looks correct to me:
<div class="editor-label">
<label for="Date">Date</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-date="The field Date must be a date." data-val-relativedate="Date must be less than or equal to 12/04/2012" data-val-relativedate-referencedate="12/04/2012 00:00:00" data-val-relativedate-relativityoperator="lessThanOrEqual" data-val-required="The Date field is required." id="Date" name="Date" type="date" value="" />
<span class="field-validation-valid" data-valmsg-for="Date" data-valmsg-replace="true"></span>
</div>
The next step was to define a custom jQuery Unobtrusive Validator adapter and method:
// Validation Method: Relative Date
// Note: method name value does NOT need to match the HTML5 metadata built by the server model annotation attribute.
$.validator.addMethod("validateRelativeDate", function (value, element, params) {
// "element" is the actual HTML element we are validating, and is unneeded here.
// "value" is the value of the HTML element
var toValidate = DateUtilities.convert(value);
// "params" is a JSON collection of those values provided by the adapter.
var referenceDate = params["referencedate"];
var relativityOperator = params["relativityoperator"];
if (relativityOperator != "lessThan" && relativityOperator != "lessThanOrEqual" || relativityOperator != "greaterThan" && relativityOperator != "greaterThanOrEqual")
return false; // "Invalid relativity operator (ex. '<', '<=', '>', or '>=').");
var toReference = referenceDate == null ? new Date() : referenceDate;
var relativity = DateUtilities.compare(toValidate, toReference);
switch(relativityOperator) {
case "lessThan":
if (relativity >= 0) return false; //"Date must be lesser than " + toReference);
break;
case "lessThanOrEqual":
if (relativity > 0) return false; //"Date must be less than or equal to " + toReference);
break;
case "greaterThan":
if (relativity <= 0) return false; //"Date must be greater than " + toReference);
break;
case "greaterThanOrEqual":
if (relativity < 0) return false; //"Date must be greater than or equal to " + toReference);
break;
}
return true;
});
// note: adapter name must match HTML metadata built by the server model annotation attribute
$.validator.unobtrusive.adapters.add("relativedate", ["referencedate", "relativityoperator"], function (options) {
options.rules["relativedate"] = options.params;
options.messages["relativedate"] = options.message;
});
Symptom
The date validation is broken. Required seems to work fine. But whenever I type any value in this field the Date type validator triggers and produces the message "The field Date must be a date". This occurs for ANY date value entered.
Question
What is wrong with my validation rule that it (1) does not work, and (2) sabotages the Date type validation?