Looking at the documentation it looks like I should be using validatedObservable
like this:
ko.validatedObservable({ id: id, name: ko.observable(name).extend({required: true}) });
But, the following line in the ko.validatedObservable
definition is causing an error:
obsv.errors.subscribe(function (errors) {
obsv.isValid(errors.length === 0);
});
If I wrap the inner object in an observable it works.
ko.validatedObservable(ko.observable({ id: id, name: ko.observable(name).extend({required: true}) }));
I can't reproduce the error on jsFiddle, but here is the example: http://jsfiddle.net/6YMsG/. I'm using Knockout 3.0 and the latest Knockout-Validation. Any suggestions?
I was able to repro the exception with configuring the validation plugin with setinng the observable
option to false
. (Demo).
Although it is not written in the documentation but you need to set observable: true
(by the way this is the default setting) in order to the ko.validatedObservable
work correctly:
So change your config to
ko.validation.init({
grouping: {
observable: true,
}
});
Writting ko.validatedObservable(ko.observable({ id: id, name: ko.observable(name).extend({required: true}) }));
does not solves this problem only hides it (in my opinion this behavious is in fact a bug in the plugin).
Because in your example:
self.id = 1;
self.name = ko.observable("blah").extend({required: true});
self.myCheck = ko.validatedObservable(ko.observable({id: self.id, name: self.name)});
the validation on name will still work but your myCheck
won't because it won't have the errors property so <span data-bind="text: myCheck.errors"></span>
will be always empty.