翻译微风验证消息

时间:2013-01-14 10:11:13

标签: breeze

改进我关于如何使用获得的元数据来创建验证规则的示例(http://stackoverflow.com/questions/13662446/knockout-validation-using-breeze-utility)现在我使用了breeze插入的验证器进入实体:

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

1 个答案:

答案 0 :(得分:3)

这是一个好主意。请将其添加到Breeze User Voice(并为其投票)。我们非常重视这些建议。

短期内还有另一种方法。您可以替换任何

Validator.messageTemplates

使用您自己的消息。 Validator.messageTemplates 是由验证程序名称键入的配置对象,其中值是错误消息的参数化版本。

我们确实需要更好地记录这一点。