这是创建验证扩展程序的代码。
addExtender: function (ruleName) {
ko.extenders[ruleName] = function (observable, params) {
//params can come in a few flavors
// 1. Just the params to be passed to the validator
// 2. An object containing the Message to be used and the Params to pass to the validator
//
// Example:
// var test = ko.observable(3).extend({
// max: {
// message: 'This special field has a Max of {0}',
// params: 2
// }
// )};
//
if (params.message) { //if it has a message object, then its an object literal to use
return ko.validation.addRule(observable, {
rule: ruleName,
message: params.message,
params: params.params || true
});
} else {
return ko.validation.addRule(observable, {
rule: ruleName,
params: params
});
}
};
},
如您所见,所有扩展程序都可以接收参数对象或带有参数和自定义消息的对象文字。所以在你的情况下。
var numberValue = ko.observable().extend({ number: {
message: "some custom message",
params: true
} })
希望这可以帮助。