I´m using KnockOut Validation to validate a Form ClientSide. Everything is working perfect except when i call the Method showAllMessages(true)
, it shows all messages in English, ignoring the locale settings. My code looks like this:
<asp:ScriptManager ID="ScriptManagerMaster" runat="server">
<Scripts>
<asp:ScriptReference Path="js/jquery-2.1.4.min.js"/>
<asp:ScriptReference Path="js/bootstrap.min.js" />
<asp:ScriptReference Path="js/knockout-3.3.0.js" />
<asp:ScriptReference Path="js/knockout.validation.js"/>
<asp:ScriptReference Path="js/es-ES.js"/>
<asp:ScriptReference Path="js/App/_run.js"/>
<asp:ScriptReference Path="js/App/App.DataModel.js"/>
<asp:ScriptReference Path="js/App/App.ViewModel.js"/>
</Scripts>
</asp:ScriptManager>
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("Se guardó el usuario");
})
.fail(self.onUserCreationError);
} else {
errors.showAllMessages(true);
}
}
When i do saveUser()
it validates correctly, but all error messages look like "This field is required
" instead of "Este campo es requerido
" as it should be since i'm using es-es
locale
How do i get it to use the locale in this case?
I went trough the ko.validation.js
file and found those messages are directly in the file and the responses are not referencing the locale.js
messages. A quick fix, was to change those messages from english to the locale.js
one manually or doing something like:
kv.rules = {};
kv.rules['required'] = {
validator: function (val, required) {
var testVal;
if (val === undefined || val === null) {
return !required;
}
testVal = val;
if (typeof (val) === 'string') {
if (String.prototype.trim) {
testVal = val.trim();
}
else {
testVal = val.replace(/^\s+|\s+$/g, '');
}
}
if (!required) {// if they passed: { required: false }, then don't require this
return true;
}
return ((testVal + '').length > 0);
},
message: 'Este campo es obligatorio.'
};