How to validate an array?

前端 未结 2 1501
灰色年华
灰色年华 2021-01-05 04:35

I am trying to use knockout validation lib to validate an array of objects. It is not straightforward to me how to form a validation group for an array of observables. The o

2条回答
  •  灰色年华
    2021-01-05 04:58

    There is a configuration option for the grouping to be deep (recursive). It can be set either globally with ko.validation.init( { grouping: { deep: true } } ) or in the group call itself, e.g.: self.errors = ko.validation.group( self.notes(), {deep: true} );

    Updated fiddle here: http://jsfiddle.net/KHFn8/4116/

    BTW, the way you did it could be written in much shorter form:

    self.errors = ko.validation.group(
        ko.utils.arrayMap(self.notes(), function(note) { return note.name }));
    

    Edit: My fiddle no longer works with the latest version of KO validation. Here is the same fiddle using the latest version at the time I gave the answer (June 2012): http://jsfiddle.net/KHFn8/4117/

提交回复
热议问题