How to make unobtrusive client-side validation work with Foolproof's ModelAwareValidationAttribute

时光毁灭记忆、已成空白 提交于 2019-12-03 17:38:11

Base on the http://foolproof.codeplex.com/SourceControl/latest#Foolproof/Client Scripts/mvcfoolproof.unobtrusive.js you can add your custom client validation rules as the server side sibling.

What I did in the projects is to extend the foolproof base on that file.

Example code:

(function () {

jQuery.validator.addMethod("foo", function (value, element, params) {
    //validation code...
});

// code based on link
var setValidationValues = function (options, ruleName, value) {
    options.rules[ruleName] = value;
    if (options.message) {
        options.messages[ruleName] = options.message;
    }
};

var $Unob = $.validator.unobtrusive;

$Unob.adapters.add("foo", ["dependentproperty", "dependentvalue", ...(add more parameters if you want)], function (options) {
    var value = {
        dependentproperty: options.params.dependentproperty,
        dependentvalue: options.params.dependentvalue,
    };
    setValidationValues(options, "foo", value);
});
})();

I hope that help you!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!