前段时间,我发布了一个辅助函数,它扩展了 observable 属性以添加基于微风验证器的验证。使用该助手,您可以在无效输入上实现红色:
翻译微风验证消息
第二个问题我无法帮助您,我知道您可以自定义验证消息,但我认为使用默认验证器您无法设置友好名称以显示在消息上。
更新:
我要做的第一件事是创建一个公开函数的辅助模块(更新版本):
define([],
function () {
"use strict";
var foreignKeyInvalidValue = 0;
function addValidationRules(entity) {
var entityType = entity.entityType,
i,
property,
propertyName,
propertyObject,
validators,
u,
validator,
nValidator;
if (entityType) {
for (i = 0; i < entityType.dataProperties.length; i += 1) {
property = entityType.dataProperties[i];
propertyName = property.name;
propertyObject = entity[propertyName];
validators = [];
for (u = 0; u < property.validators.length; u += 1) {
validator = property.validators[u];
nValidator = {
propertyName: propertyName,
validator: function (val) {
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 (i = 0; i < entityType.foreignKeyProperties.length; i += 1) {
property = entityType.foreignKeyProperties[i];
propertyName = property.name;
propertyObject = entity[propertyName];
validators = [];
for (u = 0; u < property.validators.length; u += 1) {
validator = property.validators[u];
nValidator = {
propertyName: propertyName,
validator: function (val) {
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 });
}
}
}
}
return {
addValidationRules: addValidationRules
};
});
然后,我为每个微风实体类型(http://www.breezejs.com/documentation/extending-entities)定义了一个初始化程序。例子:
define(['app/validatorHelper', 'knockout'],
function (vHelper, ko) {
"use strict";
var constructor = function () {
},
initializer = function indicadorInitializer(entity) {
vHelper.addValidationRules(entity);
};
return {
constructor: constructor,
initializer: initializer
};
});
最后,在某个地方(我在我的数据服务模块内的 init 函数上执行此操作),我正在注册初始化程序(http://www.breezejs.com/documentation/extending-entities):
//store comes from: manager = breezeconfig.createManager(),
// store = manager.metadataStore,
store.registerEntityTypeCtor("Palanca", domain.palanca.constructor, domain.palanca.initializer);
在获取元数据之前,我正在完成所有这些工作。
我希望这会对你有所帮助。
更新 2:
我发现了问题,你的淘汰验证版本不是最后一个。
在从http://ericmbarnard.github.com/Knockout-Validation/下载的文件中,第 349 行是:
exports.rules[ruleName] = ruleObj;
在您的文件中,等效行(函数 addAnonymousRule )是:
ko.validation.rules[ruleName] = {
validator: ruleObj.validator,
message: ruleObj.message || 'Error'
};
我认为最新版本应该可以工作。
更新 4:
这是要保存的代码:
vmAddPalanca.prototype.saveChangesCmd = ko.asyncCommand({
execute: function (palanca, complete) {
var validationErrors = ko.validation.group(palanca);
if (validationErrors().length === 0) {
dataservice.saveChanges()
.then(saveChangesSuccess)
.fail(saveChangesFail)
.fin(complete);
} else {
validationErrors.showAllMessages(true);
toastr.error("Debe corregir los errores antes de poder guardar");
complete();
}
},
canExecute: function (isExecuting) {
return !isExecuting && dataservice.hasChanges();
}
});