Reputation: 1767
I'm using KnockoutJS 2.2.1 along with Knockout-Validation. Is there maybe a known bug with interference between validation and creating custom extenders?
Because somehow I cannot add my custom extender. When I try to run this code:
self.from = ko.observable(new Date()).extend({ reportDate: { timeZoneValue: self.timeZoneValue} });
where reportDate is my extender, knockout-validation seems to have problem, that he can't find "rules for undefined"
Uncaught TypeError: Cannot read property 'rules' of undefined
Did somebody had this problem? How to solve it?
Here is the code for reportDate extender:
ko.extenders.reportDate = function (target, options) {
var lastValidValue = new Date();
target.subscribe(function (newValue) {
if (newValue == null) {
target(lastValidValue);
} else {
lastValidValue = newValue;
}
var isoDate = moment(DateProvider.adjustDateFromTimeZoneToUtc(target(), options.timeZoneValue())).toISOString();
target.ISODateString = isoDate;
});
};
I also wonder if knockout-validation's custom rules can break something here. Especially registerExtenders() function which is not clear for me what it does.
$(function () {
ko.validation.init({
insertMessages: true,
decorateElement: true,
errorElementClass: "error",
errorMessageClass: "validation-message",
grouping: { observable: true, deep: true }
});
ko.validation.registerExtenders();
});
Upvotes: 0
Views: 1175
Reputation: 4838
The problem is that your extender does not return any observable. From the documentation on extending observables:
It can then either return the observable or return something new like a computed observable that uses the original observable in some way.
So you should problaby add the following line as the last code line in your extender (unless you want to return a different observable object, in which case you should of course do that instead):
return target;
Upvotes: 3