Search code examples
knockout.jsknockout-validation

Knockout validation. Get Observable value


Is there some option to get all observable values from inputs? I mean to get this values:

<input type="text" data-bind="value: myValue">

I have many inputs with diffrent values, I need to get all of them and put them to the loop.

$("input[type=text]").each(function(){
       $(this).value().....
})

Any idias? Note: I need to get the observable value of my viewModel, not only the string


Solution

  • It is my understanding that you want to style elements with errors. If so, all you need to do is initialize validation with decorateElement set to true, set a css class you want to apply by setting the errorElementClass (validationElement by default) to the element and style it.

    If invalid, the style will be applied to the element.

    ko.validation.init({
        decorateElement: true,
        errorElementClass: 'error'
    });
    
    var viewModel = {
        // if missing, the 'error' class will be applied
        name: ko.observable('bob').extend({ required: true })
    };
    

    Here's a fiddle to demonstrate.