Skip to content

Allow for partial application of the message context#55

Open
xcambar wants to merge 1 commit intoadopted-ember-addons:masterfrom
xcambar:partial_message
Open

Allow for partial application of the message context#55
xcambar wants to merge 1 commit intoadopted-ember-addons:masterfrom
xcambar:partial_message

Conversation

@xcambar
Copy link

@xcambar xcambar commented Aug 19, 2017

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:

  • we were working with wrongDateFormat ("{description} must be in the format of {format}")
  • The format used was L (using the moment terminology)
  • We wanted the localised version of L ("MM/DD/YYYY" in en_US and "JJ/MM/AAAA" in fr_FR) in the message
  • We had to write this PR to allow for:
return this.formatPartialMessage(pattern, { format: localisedString});
// return value is "{description} must be in the format of JJ/MM/AAAA"

What do you think?

@offirgolan
Copy link
Collaborator

@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
Copy link
Author

xcambar commented Aug 26, 2017

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:

  • yes, language must be english, and yes, the dates must be in french
  • I need to have the message return {description} must... instead of directly returning This field must...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants