【问题标题】:Knockout Validation Plugin Custom Error Message淘汰赛验证插件自定义错误消息
【发布时间】:2013-05-27 01:58:08
【问题描述】:

基于以下内容,我该如何设置回调以显示自定义错误消息而不是默认消息?

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'
};

【问题讨论】:

    标签: knockout-validation


    【解决方案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'
    };
    

    【讨论】:

    • 函数ko.validation.rules['exampleAsync']什么时候被调用
    • @SrinivasNaidu 这不是一个函数。现在,validator 函数将在更改通过 extend 附加验证的 observable 时被调用。
    猜你喜欢
    • 1970-01-01
    • 2014-05-28
    • 2013-08-29
    • 2014-02-21
    • 1970-01-01
    • 2012-05-20
    • 2013-07-01
    • 2015-01-08
    • 2012-08-19
    相关资源
    最近更新 更多