Saturday, 15 June 2013

knockout.js - Knockout Validation: Unexpected behavior when using collections of validatedObservables -



knockout.js - Knockout Validation: Unexpected behavior when using collections of validatedObservables -

i'm seeing unexpected behavior when attempting validate view model comprised of collections of validatedobservable objects; actually, nested collections.

first, allow me point jsfiddle demonstrates behavior: http://jsfiddle.net/cbono/ztxb9/8/

my view model app object. contains single property, services, collection of service objects. each service contains retailers property which, guessed it, collection of retailer objects. each object instantiated validatedobservable can phone call isvalid on top level object , find any/all errors in kid objects.

when init library, use:

ko.validation.init({ grouping: { deep: true, observable: true } });

the validation rule have in effect on retailer objects. if retailer selected , retailer's name "other" ((isselected() && isother()) === true), otherdesc property required. that's it. otherwise, retailer valid.

when test rule (check 1 of "other" checkboxes , leave accompanying description field blank), validation fails expected. however, view model's errors collection contains this:

[undefined, undefined, "this field required"]

further, error on js console when self.errors.showallmessages() called:

uncaught typeerror: object function d(){if(0<arguments.length) {if(!d.equalitycomparer||!d.equalitycomparer(c,arguments[0]))d.h(),c=arguments[0],d.g();return this}b.r.wa(d);return c} has no method 'ismodified'

i'm pretty sure these 2 problems related, haven't been able piece what's happening behind scenes. seems when validation library walking object graph, it's getting hold of objects not right type (hence no ismodified method) , placing bunch of undefined's in errors collection.

am not using library intended? i'm trying hard work because thought of model validation. much more complicated recreate validation in jquery.validate.

it looks validatedobservable's can not nested. leaving app validatedobservable solves problem.

self.services = ko.observablearray( ko.utils.arraymap(data, function (svc) { homecoming new service(svc); // disclose })); //.... self.retailers = ko.observablearray( ko.utils.arraymap(data.retailers, function (rtl) { homecoming new retailer(rtl); // disclose }));

http://jsfiddle.net/ostgals/ztxb9/16/

knockout.js knockout-validation

No comments:

Post a Comment