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

有帮助吗?

解决方案

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;
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top