I'm trying to add a custom validator using the MVC Foolproof library.
However, upon following the instructions here I get the error message "Sys is not defined".
I suspect this is because the library was originally written to work with the older validation script files which came with MVC 2 (and possibly 3), so is there a way to register my validation function for the javascript validation files which come with MVC 4?
The property on my model is defined as
[RatingTextRequired(ErrorMessageResourceType = typeof(ModelResources.Customer.Order.FeedbackRating), ErrorMessageResourceName = "RequiredError_Comment")]
public string FeedbackText { get; set; }
The validation attribute is defined as
public class RatingTextRequiredAttribute : ModelAwareValidationAttribute
{
//this is needed to register this attribute with foolproof's validator adapter
static RatingTextRequiredAttribute()
{
Register.Attribute(typeof (RatingTextRequiredAttribute));
}
public override bool IsValid(object value, object container)
{
var model = (Areas.Customer.Models.FeedbackRating) container;
return !(String.IsNullOrWhiteSpace(value.ToString()) && (model.WantsPostEditing || model.Rating == "0"));
}
}
And the JavaScript wiring-up for this is
Sys.Mvc.ValidatorRegistry.validators["ratingtextrequired"] = function (rule) {
return function(value, context) {
var wantsPostEditingProp = foolproof.getId(context.fieldContext.elements[0], "WantsPostEditing");
var wantsPostEditingVal = document.getElementById(wantsPostEditingProp).value;
var ratingProp = foolproof.getId(context.fieldContext.elements[0], "Rating");
var ratingVal = document.getElementById(ratingProp).value;
return !(value.length == 0 && (wantsPostEditingVal || ratingVal === "0"));
};
};
Also, I'm not sure what the file MvcFoolproofJQueryValidation.js is requied for because somehow I had the out-of-the-box validation working without this script file being present. Sadly, including it doesn't help with my "Sys is not defined" error.