Being able to override attributeName #148
Description
Hello,
I have the following problem: I would like to be able to override the attribute name in the error messages.
For example, I have a countryCurrency field in my form, that's a business term, but my application is in french, and the label displayed over the field is 'Devise du pays'.
Using validate.js, I get the error message 'CountryCurrency is invalid' (actually we translated the 'is invalid' part), which is at best not very pretty, but also can be confusing for our users.
I would like to be able to do this:
validate({ countryCurrency: '€' }, {
countryCurrency: {
attributeName: 'Devise du pays',
presence: true
}
})
So I can have a nice error message 'Devise du pays is invalid'.
I realize the attribute name is not a constraint and has not much to do with it, but that's the most logical place I could find. Maybe you could rename the parameter 'contraints' to 'rules', which would make more sense if attributeName is added to it.
Writing this, I'm thinking that I could possibly do this:
// I haven't tested this code
validate({ 'Devise du pays': '€' }, {
'Devise du pays': {
presence: true
}
})
But you see, I'm leveraging validate.js by linking it to redux-form. I suppose you're not familiar with it, but it happens that the errors object returned by default by validate.js (grouped) fits perfectly what redux-form is expecting. I'm doing this:
var constraints = 'REDACTED';
var validator = function (values) {
// values is passed by redux-form and is a plain object of { field-name: value }
// just what validate.js needs
return validate(values, constraints);
};
...and give validator to redux-form. That works very well, except for this attribute name problem.
I can see I'm not the only one having this problem:
#69 #102
But proposed solutions so far does not seem very nice to me.
I could do a PR of this if you're interested. I'm also considering to write a TypeScript definition for validate.js, since it's missing and would be useful.