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

Allow a function als validation message

Open hoetmaaiers opened this issue 10 years ago • 2 comments
trafficstars

By allowing a function in the validationMessage binding, it is possible to for example use another knockout observable as the validation message. The following example is used in our application for dynamically updating the validation message. I18n is provided by i18next.js for the whole application.

required: {
    message: function() {
      return i18n.get('validation_required')
    }
}

Not sure if any tests are written for this custom binding?

hoetmaaiers avatar Oct 20 '15 12:10 hoetmaaiers

Seems that this PR is not appropriate anymore, while playing around and trying to reimplement this, found appropriate tests, so if you wish to use messages as functions you may do something like this:

self.dateBirth = ko.observable().extend({
    required: {
        params: true,
        message: function (params, observable) {
            return self.resources.errorMessageDateBirthRequired.label();
        }
    }
});

mac2000 avatar May 30 '16 22:05 mac2000

Thanks @mac2000, I needed to add i18n support to validation messages and your solution works like a champ!

phazlett avatar Dec 04 '16 05:12 phazlett