Knockout Validation on Array
Asked Answered
H

2

10

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!

Horn answered 26/10, 2012 at 19:24 Comment(1)
Knockout appears to be having an issue with the manner in which the objects are added to the array. If the objects are created within the array definition, and removed from the load method, it works as expected.Evalynevan
J
3

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.

Jermyn answered 10/11, 2012 at 0:18 Comment(0)
L
5

I had the same problem and it can be solved by setting the observable config property to false. This triggers deep validation every time the errors function is called:

    ko.validation.init({
        grouping: {
            deep: true,
            observable: false //important ! Needed so object trees are correctly traversed every time so added objects AFTER the initial setup get included
        },
        insertMessages: true,
        messagesOnModified: true,
        debug: false
    });
Lawley answered 24/5, 2013 at 13:18 Comment(0)
J
3

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.

Jermyn answered 10/11, 2012 at 0:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.