Skip to content

Using validation with a custom binding

homer2 edited this page Jun 10, 2015 · 2 revisions

First, Create the custom binding, such as...

ko.bindingHandlers.yourBindingName = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here
    },
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called once when the binding is first applied to an element,
        // and again whenever any observables/computeds that are accessed change
        // Update the DOM element based on the supplied values here.
    }
};

... then register the custom binding with knockout validation:

ko.validation.makeBindingHandlerValidatable('yourBindingName');