aurelia / validation

A validation plugin for Aurelia.
MIT License
132 stars 129 forks source link

Error messages do not let us translate the attribute #281

Closed gravsten closed 8 years ago

gravsten commented 8 years ago

If I have a property of my model called "password" to be validated, while I can pass the translated error messages in the options, the attribute never gets translated.

To solve this problem, I have changed on line in validate.js (around line 426):

  convertErrorMessages: function(errors, options) {

... error = v.capitalize(v.prettify(errorInfo.attribute)) + " " + error;

needs to be changed to: error = v.capitalize(v.prettify(errorInfo.options.tr || errorInfo.attribute)) + " " + error;

While I considered naming the option 'attribute', this would have collided with other uses (e.g. in equality validator). So I went for 'tr', which refers to the i18n.tr() function.

As a result, the attribute name gets properly translated for the user.

gravsten commented 8 years ago

As a follow up, there was one more change needed especially for the 'equality' validator: if (!comparator(value, otherValue, options, attribute, attributes)) { return v.format(message, {attribute: v.prettify(options.attribute_tr || options.attribute)}); }

Please note the additional 'options.attribute_tr || ' above, which let us translate the linked attribute.

gheoan commented 8 years ago

https://github.com/ansman/validate.js/issues/148

gravsten commented 8 years ago

Here is an example use of the above changes to support translated attribute: @required({ tr: i18n.tr('email'), message: i18n.tr('validatejs.presence.message') })

And another for the special case of @equality with two attributes: @equality({ attribute: 'new_password_1', attribute_tr: i18n.tr('new_password'), tr: i18n.tr('confirmation'), message: i18n.tr('validatejs.equality.message') })

gravsten commented 8 years ago

I have updated the aforementioned proposed changes to avoid prettifying the provided translations: error = v.capitalize(errorInfo.options.tr || v.prettify(errorInfo.attribute)) + " " + error; and for @equality: return v.format(message, {attribute: options.attribute_tr || v.prettify(options.attribute)});

This proposal has the merit of being simple, working now, and compatible with all existing Aurelia applications (if you don't use the 'tr' option, the attributes will just keep not being translated). But I would be glad to hear about other possible solutions for translating validation attributes.

jdanyow commented 8 years ago

this is now possible- check out the examples in the docs.

Strato commented 8 years ago

this is now possible- check out the examples in the docs.

@jdanyow Possible how? What doc are you talking about? Validate.js or Aurelia?

Can you provide a link please?

jdanyow commented 8 years ago

Validatejs is no longer used.

http://aurelia.io/hub.html#/doc/article/aurelia/validation/latest/validation-basics