Knockout-Validation
Knockout-Validation copied to clipboard
Locale not loading on errors.showAllMessages(true)
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.
@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.
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.
Here you go: http://jsfiddle.net/juandozco/v9L69g8a/9/ i just added the locale es-ES
to 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.
@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)
Defining localizations with localize
doesn't seem to work either: http://jsfiddle.net/x9bsebuh/
@eugenyshchelkanov It works but it needs to occur before validation.
Updated example http://jsfiddle.net/x9bsebuh/1/