adopted-ember-addons / ember-validators

A collection of EmberJS validators
Other
24 stars 40 forks source link

Allow for partial application of the message context #55

Open xcambar opened 7 years ago

xcambar commented 7 years ago

This PR allows, with a backward compatible modification, to let the developers partially apply a context to a message.

This is very useful for instance when extending the messages provided by ember-validations to reuse messageFor to do the extension/replacement instead of doing a .replace(...) manually, which might break eventually...

Down to earth example:

What do you think?

offirgolan commented 7 years ago

@xcambar not sure I follow you here. Where is this partial message being created? Why is it needed? and what are you doing with it after?

xcambar commented 7 years ago

Here's what I can achieve with this PR:

    validator('date', {
      precision: 'day',
      message: function(err, input, validator) {
        let localizedFormat = moment().localeData().longDateFormat(validator.get('format')); // => DD/MM/YYYY because lang == 'fr_FR'
        let translatedFormat = t(localizedFormat); // => JJ/MM/AAAA in french
        return validatorsMessages.formatPartialMessage(wrongDateFormat, { format: localizedFormat });
        // => {description} must be in the format of JJ/MM/AAAA
      },
      format: 'L'
    })

With the current master of ember-validators, I didn't succeed in having the error message to be This field must be in the format of JJ/MM/AAAA. If you can provide some guidance towards this with a simple solution, then yes, this PR may be superfluous.

A couple of notes: