改进我的示例,了解如何使用获得的元数据在淘汰赛中创建验证规则 (http://stackoverflow.com/questions/13662446/knockout-validation-using-breeze-utility) 现在我使用轻风插入实体的验证器:
function addValidationRules(entity) {
var entityType = entity.entityType;
console.log(entityType);
if (entityType) {
for (var i = 0; i < entityType.dataProperties.length; i++) {
var property = entityType.dataProperties[i];
var propertyName = property.name;
var propertyObject = entity[propertyName];
var validators = [];
for (var u = 0; u < property.validators.length; u++) {
var validator = property.validators[u];
var nValidator = {
propertyName: propertyName,
validator: function (val, other) {
var error = this.innerValidator.validate(val, { displayName: this.propertyName });
this.message = error ? error.errorMessage : "";
return error === null;
},
message: "",
innerValidator: validator
}
validators.push(nValidator);
}
propertyObject.extend({
validation: validators
});
}
for (var i = 0; i < entityType.foreignKeyProperties.length; i++) {
var property = entityType.foreignKeyProperties[i];
var propertyName = property.name;
var propertyObject = entity[propertyName];
var validators = [];
for (var u = 0; u < property.validators.length; u++) {
var validator = property.validators[u];
var nValidator = {
propertyName: propertyName,
validator: function (val, other) {
var error = this.innerValidator.validate(val, { displayName: this.propertyName });
this.message = error ? error.errorMessage : "";
return error === null;
},
message: "",
innerValidator: validator
}
validators.push(nValidator);
}
propertyObject.extend({
validation: validators
});
if (!property.isNullable) {
//Bussiness Rule: 0 is not allowed for required foreign keys
propertyObject.extend({ notEqual: foreignKeyInvalidValue });
}
}
}
};
我现在需要将错误消息翻译成我的语言,我想知道是否可以包含一个类似于包含在敲除验证中的功能来翻译消息:
//quick function to override rule messages
ko.validation.localize = function (msgTranslations) {
var msg, rule;
//loop the properties in the object and assign the msg to the rule
for (rule in msgTranslations) {
if (ko.validation.rules.hasOwnProperty(rule)) {
ko.validation.rules[rule].message = msgTranslations[rule];
}
}
};
//#endregion