hotdiggity
hotdiggity

Reputation: 329

KnockoutJS Validation binding value passed to custom binding?

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/

Upvotes: 0

Views: 92

Answers (1)

Anders
Anders

Reputation: 17554

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());

Upvotes: 1

Related Questions