Knockout Validation Plugin Custom Error Message

故事扮演 提交于 2019-12-21 04:30:14

问题


Based on the following, how exactly do I setup the callback to display a custom error message instead of the default message?

ko.validation.rules['exampleAsync'] = {
    async: true, // the flag that says "Hey I'm Async!"
    validator: function (val, otherVal, callBack) { // yes, you get a 'callback'

        /* some logic here */

        // hand my result back to the callback
        callback( /* true or false */ );
        // or if you want to specify a specific message
        callback( /* { isValid: true, message: "Lorem Ipsum" } */ );
    },
    message: 'My default invalid message'
};

回答1:


ko.validation.rules['exampleAsync'] = {
    async: true, 
    validator: function (val, otherVal, callBack) { 

        // make an ajax call or something here to do your async validation 
        $.ajax({ type: 'post', url: 'some url', data: val, success: function (data) { 
            if (data.success) {
                callback({ isValid: true, message: "yay it worked"});
            } else {
                callback({ isValid: false, message: data.message });
            }
        });
    },
    message: 'My default invalid message'
};


来源:https://stackoverflow.com/questions/12611878/knockout-validation-plugin-custom-error-message

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