Question

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

Was it helpful?

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;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top