I am trying to write a very simple Data Annotation Validator for my ASP.NET MVC viewmodel. When applied on a datetime field, the validator must check whether the year of the date is the current year or not. Server side works as intented, but client side doesn't seem to trigger (other validators like range or required work)
Server Side:
class CurrentYearAttribute : ValidationAttribute, IClientValidatable
{
public override bool IsValid(object value)
{
if(value == null)
{
return false;
}
var date = (DateTime)value;
return (date.Year == DateTime.Now.Year);
}
public override string FormatErrorMessage(string name)
{
return "Le champ " + name + " doit être de l'année en cours.";
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule()
{
ValidationType = "currentyear",
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
};
yield return rule;
}
}
Client side:
<script type="text/javascript">
$(document).ready(function () {
$.validator.addMethod(
'currentyear',
function (value, element, params) {
alert("hello"); // never
return (Date.parse(value).getFullYear() == (new Date()).getFullYear());
});
$.validator.unobtrusive.adapters.addBool("currentyear");
//$.validator.unobtrusive.parse("form");
});
</script>
The problem is just that you have not defined the rule part of your adapter.
Try using something like this:
$.validator.unobtrusive.adapters.addBool("currentyear", function (options) {
options.rules["currentyear"] = "#" + options.element.name.replace('.', '_'); // mvc html helpers
options.messages["currentyear"] = options.message;
});
About the rule :
The jQuery rules array for this HTML element. The adapter is expected to add item(s) to this rules array for the specific jQuery Validate validators that it wants to attach. The name is the name of the jQuery Validate rule, and the value is the parameter values for the jQuery Validate rule.