美好的一天,
我正在尝试验证一个非常庞大的表单,并且基本上重写了我的所有viewModel代码,以便可以验证它。问题是ko.validatedObservable()由于某种原因似乎不存在。我使用了this site中的代码,老实说,我无法看到代码中的问题。这是一个示例:
var someNameSpace= {};
someNameSpace.bindData = function () {
someNameSpace.viewModel =
someNameSpace.initViewModel("123456", "username1", "address1", "address2", "city");
ko.applyBindings(this.viewModel);
}
someNameSpace.initViewModel = function (partnerId, username, address1, address2, city) {
console.log(ko);
var someViewModel = ko.validatedObservable({ //<----FAILS HERE
partnerId: ko.observable(partnerId).extend({ required: "This field is required", number: true }),
username: ko.observable(username).extend({ required: "This field is required" }),
address1: ko.observable(address1).extend({ required: "This field is required" }),
address2: ko.observable(address2).extend({ required: "This field is required" }),
city: ko.observable(city).extend({ required: "This field is required" }),
});
var validationOptions = { insertMessages: true, decorateElement: true };
ko.validation.init(validationOptions);
return someViewModel ;
}
$(document).ready(function () {
someNamespace.bindData();
});
在该网站上,它声称使用ko.validatedObservable来检查用户是否正确输入了字段。我仍然是淘汰赛的新手 - 我该怎么办?
答案 0 :(得分:3)
您需要包含对'knockout.validation.js'
库的引用。
<强> knockout.validation.js 强>
https://rawgit.com/ericmbarnard/Knockout-Validation/master/Src/knockout.validation.js
GitHub参考