문제

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