1

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.

awj
  • 7,482
  • 10
  • 66
  • 120

1 Answers1

0

Base on your error you are using the jquery obstructive approach.

See this post for more info.

Community
  • 1
  • 1
joaoasrosa
  • 301
  • 1
  • 17