Knockout-Validation icon indicating copy to clipboard operation
Knockout-Validation copied to clipboard

Locale not loading on errors.showAllMessages(true)

Open Whoandozco opened this issue 8 years ago • 7 comments

Hi, i think there's a bug related to the locale settings when you call the method showAllMessages(). I'm using es-ES locale in one of my apps with ko.validation, but when i call this method, it wont show the messages in spanish as it should, but in english, ignoring locale settings. My code looks like this:

ko.validation.locale("es-es");
    ko.validation.init({
        registerExtenders: true,
        messagesOnModified: true,
        insertMessages: true,
        parseInputAttributes: true,
        errorElementClass: "has-error",
        errorMessageClass: "help-block",
        decorateInputElement: true
    }, true);

self.saveUser = function () {
        var errors = ko.validation.group(self, { deep: true });
        if (errors().length === 0) {
            var user = ko.toJSON(self);
            dataModel.putUser(user)
                .done(function(result) {
                    alert("User saved");
                })
                .fail(self.onUserCreationError);
        } else {
            errors.showAllMessages(true);
        }
    }

messages will display if there are errors in the saveUser() call, but they wont be in the language it was set in the ko.validation.locale

EDIT:

Looking at ko.validation code, i noticed this messages are just there, in each of the validations, that is why they are not showing using the locale settings.

Whoandozco avatar Jul 17 '15 13:07 Whoandozco

@Juandozco Please ensure the required files are loaded. Some things to check:

  • https://github.com/Knockout-Contrib/Knockout-Validation/wiki/Localization
  • http://jsfiddle.net/crissdev/o1hp49vd/ (example using script tags)
  • http://jsfiddle.net/crissdev/pt6fay6d/ (example using RequireJS)

If that won't help then create an example on jsfiddle.net and we'll start from there. Thanks.

crissdev avatar Jul 17 '15 15:07 crissdev

I did that, I noticed the message i was getting, was directly written in the code, not being loaded from the locale file. Take a look at line 755 at kv.rules['required'], that rule isn't loading a locale. Just giving the default english message as is.

Whoandozco avatar Aug 03 '15 16:08 Whoandozco

@Juandozco Can you post a repro please, jsbin or jsfiddle? Thanks.

crissdev avatar Aug 03 '15 17:08 crissdev

Here you go: http://jsfiddle.net/juandozco/v9L69g8a/9/ i just added the locale es-ESto that fiddle i did like last week. Press Save User button and you'll get the messages in English, even tho the locale is set to spanish. Then try writing a wrong e-mail address and you'll get the message in spanish. As i told you, this is because those messages are written directly in the code, not being called from the locale file.

Whoandozco avatar Aug 03 '15 21:08 Whoandozco

@Juandozco Working version http://jsfiddle.net/v9L69g8a/10/ You have to change current language before validating any observable. Otherwise you'll have to call valueHasMutated or notifySubscribers to re-validate it. Take a close look at this example and notice how valueHasMutated is called after the locale switch call.


Live localization is not yet supported (#158)

crissdev avatar Aug 04 '15 11:08 crissdev

Defining localizations with localize doesn't seem to work either: http://jsfiddle.net/x9bsebuh/

eugenyshchelkanov avatar Aug 31 '15 11:08 eugenyshchelkanov

@eugenyshchelkanov It works but it needs to occur before validation.

Updated example http://jsfiddle.net/x9bsebuh/1/

crissdev avatar Aug 31 '15 11:08 crissdev