Open arb opened 9 years ago
There is no such feature today I'm afraid. You'd have to do this manually. I'll consider implementing a way of this but it sounds like a specialised use case.
I think this is important too +1
This is really important I think. E.g. email
attribute is printed as Email
instead of E-mail
. Just add an attribute label
in the constraints object and print it in the error.
You could just override validate.prettify
and do what ever aliasing you want there.
I solve it in my fork https://github.com/uiii/validate.js. But I didn't update the documentation and fully complete the tests, so I didn't make a PR yet.
I've passed the labels to the options to validate
function like this:
validate(attributes, constraints, {
labels: {
<attr-name>: "Attr label"
}
)
Overriding validate.prettify
is straightforward enough, but the shortcoming of that approach is that prettify is scoped globally and doesn't know the context in which it's been called, while your aliases are probably scoped to the data you're validating. That's to say that you may require different aliases for one model than you require for another, and prettify doesn't provide a mechanism for knowing which is which.
I need this feature as well, it's hard to support multiple languages, or even code in english and have it display something else.
I made a PR that could solve this issue by allowing custom prettify
to be specified in the validate options instead of globally: #164
Any progress on this?
@thj-dk I've been waiting on a response to my PR 😢
Mind if I bump this a little?
bump
I'd also love to see this feature!
@williamdwarke It's been merged and released with v0.12.0, you can now override prettify
via your validation options.
This seems like a big oversight. It's common to have attribute names that aren't human-readable and you can't change, particularly if using a 3rd party service to process your form data (Mailchimp for example)
Furthermore, validate seems to automatically include the attribute name in the message (instead of using some sort of template string to allow for complete replacement) so you can't overwrite it that way either.
Finally, the validate function does automatically perform some sort of formatting on the attribute name before displaying (i.e. my_field
gets changed to My field
), so you can't easily find and replace the attribute within the outputted errors from the validate function.
Edit: here's a rough-and-ready function I've used to remap the errors that might help someone:
formatErrors(errors, mapping) {
for (var attrName in errors) {
if (errors.hasOwnProperty(attrName)) {
for (let i = 0; i < errors[attrName].length; i++) {
var searchStr = validate.prettify(attrName);
searchStr = searchStr.charAt(0).toUpperCase() + searchStr.slice(1);
errors[attrName][i] = errors[attrName][i].replace(
token,
errorMapping[attrName]);
}
}
}
return errors;
}
var errors = validate(...);
formatErrors(errors, {
"merge_fields[FNAME]": "First name",
"merge_fields[LNAME]": "Last name",
"email": "Email",
"merge_fields[MMERGE12]": "Zip code"
})
// Input:
// "Merge field[fname] can't be blank"
//
// Output:
// First name can't be blank
@timmyomahony Could you clarify why something like this wouldn't solve your issues with attribute name formatting?
const aliases = {
"merge_fields[FNAME]": "First name",
"merge_fields[LNAME]": "Last name",
"email": "Email",
"merge_fields[MMERGE12]": "Zip code"
};
return validate(values, constraints, {
prettify: function prettify(string) {
return aliases[string] || validate.prettify(string);
},
});
@ansman what is the status?
@mindaugasnakrosis My PR for local prettify
config has been shipped already, please specify why the sample I posted is insufficient for you.
Is there a way to set the attribute name so the automatically generated messages would use "alias" instead of the "attribute" name? This would drastically reduce the number of
message
values needed.