Search code examples
arraysknockout.jsknockout-validation

Knockout Validation on Array


I am using Knockout Validation to validate a field in an array. It will display the error message, but I cannot get isValid() or ko.validation.group() to work. I need one of these to handle the submit.

errors = ko.validation.group(contactList(), {deep:true});

Here is the fiddle: http://jsfiddle.net/mduey/hEJWJ/80/

Thanks!


Solution

  • The trouble is that validation is not run when you add new array elements to your observableArray. Here is quick an dirty solution which can be optimized further.

    Every time you add a new element to your observableArray I run validation, putting it's results into observable so it can be properly tracked by view bindings. It can be further improved with throttle setting, so validation will not run for every push to array (actual for cycles). As I see currently ko.validation.group returns not a proper observable as it should, so I had to create a wrapper for it.