我是使用剔除js验证插件的新手。我的观测值在另一个函数上。但似乎User.errors = ko.validation.group(User)和User.errors = ko.validatedObsevable(User)无法读取错误/无法验证我的可观察对象。
我尝试了一些小提琴代码:http://jsfiddle.net/KHFn8/3238/
在该示例中,条件和可观察变量在同一函数上。
这是我的js代码:
var User = function () {
this.UserId = ko.observable();
this.Fullname = ko.observable().extend({ required: true });
this.Username = ko.observable().extend({ required: true });
this.StartedDate = ko.observable().extend({ required: true });
this.Email = ko.observable().extend({ required: true });
this.Facebook = ko.observable().extend({ required: true });
this.Address = ko.observable().extend({ required: true });
this.MobileNumber = ko.observable().extend({ required: true });
this.Birthdate = ko.observable().extend({ required: true });
this.PositionId = ko.observable().extend({ required: true });
}
UserModel function {
self.Save = function () {
if (User.errors().length == 0) {
alert(User.errors().length);
} else {
alert('Please check your submission.');
User.errors.showAllMessages();
}
}
User.errors = ko.validation.group(User);
ko.applyBindings(new userModel());
我只想验证必填字段并在“提交/保存”按钮上显示错误消息
答案 0 :(得分:0)
好吧,尝试了3天后。我想出了什么答案。
我重新放置User.errors = ko.validation.group(User);发挥作用
var User = function(){
...
this.errors = ko.validation.group(this); //change 'user' to 'this'
}
UserModel function {
self.Save = function () {
if (this.errors().length == 0) {
alert(this.errors().length);
} else {
alert('Please check your submission.');
}
}
}
ko.applyBindings(new userModel());