Search code examples
jqueryknockout.jsknockout-validation

insertValidationMessage knockout not working


i am using knockout validation i want to show custom message by my own function here is my code for ko validations

  ko.validation.init({

   // registerExtenders: true,
    messagesOnModified: false,
    insertMessages: true,
    parseInputAttributes: true,
    messageTemplate: null,
    grouping: { deep: true, observable: true },
    registerExtenders: true,
    insertValidationMessage: function (element) {

        var span = document.createElement('SPAN');
        span.className = "validationMessage";

        if ($(element).hasClass("error-before"))
            element.parentNode.insertBefore(span, element);
        else
            element.parentNode.insertBefore(span, element.nextSibling);

        return span;
    }
});

but insertvalidationmessage is not being called Am i doing any thing wrong


Solution

  • The init method only supports overriding configuration properties, so if you want to override methods you need to do it directly on the ko.validation object.

    So your initialization code should look like this:

    ko.validation.init({
       // registerExtenders: true,
        messagesOnModified: false,
        insertMessages: true,
        parseInputAttributes: true,
        messageTemplate: null,
        grouping: { deep: true, observable: true },
        registerExtenders: true
    });
    
    ko.validation.insertValidationMessage = function (element) {
    
        var span = document.createElement('SPAN');
        span.className = "validationMessage";
    
        if ($(element).hasClass("error-before"))
           element.parentNode.insertBefore(span, element);
        else
           element.parentNode.insertBefore(span, element.nextSibling);
    
        return span;
    };