Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setMessage to accept a map of rule / message (or add a setMessages method) #163

Open
PacciniBruno opened this issue Oct 22, 2015 · 1 comment

Comments

@PacciniBruno
Copy link

The use case would be to allow for easy internationalisation: language detection and passing of messages is handled outside validate, but setMessage should allow for easy passing of locales:

For example, setMessage could be modified as follow (I can create a PR with a complete solution + tests + documentation update if the issue is considered relevant):

FormValidator.prototype.setMessage = function(rule, message) {
    var that = this;

    if (!message && typeof rule === 'object' && !!rule) {
      Object.keys(rule).forEach(function(key) {
        that.messages[key] = rule[key];
      });
    } else {
      that.messages[rule] = message;
    }

    // return this for chaining
    return this;
};

And use:

// My object containing all localised copy:
var i18n = {};

i18n.validateJs = {
  required: 'Le champ %s doit être rempli.',
  matches: 'Le champ %s n\'est pas égal au champ %s.',
  "default": 'Le champ %s a toujours sa valeur par défaut. Veuillez la changer.',
  valid_email: 'L\'adresse renseignée doit être une adresse email valide.',
};

// SetMessage:
this.validator.setMessage(i18n.validateJs)
@rickharrison
Copy link
Owner

Personally, I'd be interested in seeing a companion module that is a standalone function, which could take in an internationalization object and then call the right methods on validate.js to achieve the desired effect.

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

No branches or pull requests

2 participants