Knockout Validation Plugin Custom Error Message
Asked Answered
B

1

7

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'
};
Breastwork answered 26/9, 2012 at 23:17 Comment(0)
R
5
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'
};
Rank answered 26/5, 2013 at 17:58 Comment(2)
When will be the function ko.validation.rules['exampleAsync'] calledBlackstock
@Blackstock It's not a function. Now, validator function will be called upon change of the observable to which the validation was attached via extend.Geometric

© 2022 - 2024 — McMap. All rights reserved.