我想做一个密码更改表单。它应该随时显示在设置页面上,不需要填写。但是,如果三个字段(旧通行证,新通行证,确认)中至少有一个已填写,则需要填写其他字段。
我能做到吗?
这是我的临时代码
self.oldPass = ko.observable().extend({ minLength: 6, maxLength: 20, validation: { validator: checkEqual, message: 'Неверный пароль.', params: self.password } });
self.newPass = ko.observable().extend({ minLength: 6, maxLength: 20, validation: { validator: newPassValid, message: 'Неверный пароль.' } });
self.newPassCheck = ko.observable().extend({ minLength: 6, maxLength: 20, validation: { validator: checkEqual, message: 'Пароли не совпадают.', params: self.newPass } });
function newPassValid(pass){
}
function checkEqual(val, other){
return val == other();
}
您可以尝试使用onlyIf:
进行条件验证。self.oldPass = ko.observable().extend({
required: {
onlyIf: function () {
var isNewPassFilled = self.newPass() && self.newPass().length > 0;
var isNewPassCheckFilled = self.newPassCheck() && self.newPassCheck().length > 0;
return isNewPassFilled || isNewPassCheckFilled;
}
},
minLength: 6,
maxLength: 20,
validation: {
validator: checkEqual,
message: 'Wrong password',
params: self.password
},
});