4

Improving my example on how to use the metadata obtained to create validation rules in knockout (http://stackoverflow.com/questions/13662446/knockout-validation-using-breeze-utility) now I use the validators that breeze inserts into the entities:

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 });
            }
        }
    }
};

What I need now is to translate the error messages into my language and I was wondering if it would be possible to include a function to breeze similar to the included in knockout-validation to translate messages:

//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 Answer 1

4

This is a good idea. Please add it to the Breeze User Voice ( and vote for it). We take these suggestions very seriously.

There is another approach for the short term. You can replace any of the

Validator.messageTemplates

with your own messages. Validator.messageTemplates is a configuration object keyed by the name of the validator where the value is a parameterized version of the error message.

We do need to document this better.

5
  • Ths suggestion is added: breezejs.uservoice.com/forums/173093-breeze-feature-suggestions/… Jan 14, 2013 at 18:20
  • I had seen this configuration object, but at a pace that breeze is updated seemed tedious to have to go replacing those lines every time. Jan 14, 2013 at 18:23
  • You don't have to touch the source for this, you can just update them directly after breeze is loaded. Jan 14, 2013 at 19:19
  • 1
    Julian, please re-read the API documentation of messageTemplates and help us understand what we are missing. We think your needs are covered if you call your own, custom message template configuration code when your application launches. Such configuration would have lines such as Validator.messageTemplates["required", "'%displayName%' is required") Of course you would say it in your language.
    – Ward
    Jan 15, 2013 at 8:26
  • Sorry, it was my fault. I did not read the api (or the section: "Customize the message templates"of breezejs.com/documentation/validation) and I thought that "Validator.messageTemplates" was an internal property that could not be changed. Now I see clearly that it is not necessary nothing more. Jan 15, 2013 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.