KnockoutJS Validation binding value passed to custom binding?

淺唱寂寞╮ 提交于 2019-12-25 01:54:02

问题


Using KnockoutJS-Validation, I need to pass to a custom binding whether the field passed validation or not. Guessing I need to somehow hook into a KnockoutJS-Validation observable at field level using the allBindingsAccessor parameter but unsure how.

ko.bindingHandlers.mycustombinding = {
    update: function (element, valueAccessor, allBindingsAccessor) {
        allBindings = allBindingsAccessor(),
        validationObservable = allBindings.validationObservable;    
        if (!validationObservable) {
            //do cool jQuery stuff to the element if it doesn't validate
        }
    }
};

http://jsfiddle.net/hotdiggity/mtwLA/6/


回答1:


The library adds a obserabler isValid to the observable thats extended

http://jsfiddle.net/MCNU8/

var observable = ko.observable("f").extend({ number: true });
console.log(observable.isValid());


来源:https://stackoverflow.com/questions/21405518/knockoutjs-validation-binding-value-passed-to-custom-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!