ansman / validate.js

A declarative validation library written javascript
https://validatejs.org
MIT License
2.63k stars 336 forks source link

Set attribute name #69

Open arb opened 9 years ago

arb commented 9 years ago

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.

ansman commented 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.

bradwestfall commented 8 years ago

I think this is important too +1

uiii commented 8 years ago

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.

ansman commented 8 years ago

You could just override validate.prettify and do what ever aliasing you want there.

uiii commented 8 years ago

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"
    }
)
michaek commented 8 years ago

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.

Saturate commented 8 years ago

I need this feature as well, it's hard to support multiple languages, or even code in english and have it display something else.

vdh commented 8 years ago

I made a PR that could solve this issue by allowing custom prettify to be specified in the validate options instead of globally: #164

thj-dk commented 7 years ago

Any progress on this?

vdh commented 7 years ago

@thj-dk I've been waiting on a response to my PR 😢

mrchimp commented 7 years ago

Mind if I bump this a little?

bump

williamdwarke commented 6 years ago

I'd also love to see this feature!

vdh commented 6 years ago

@williamdwarke It's been merged and released with v0.12.0, you can now override prettify via your validation options.

timmyomahony commented 4 years ago

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
vdh commented 4 years ago

@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);
  },
});
mindaugasnakrosis commented 4 years ago

@ansman what is the status?

vdh commented 4 years ago

@mindaugasnakrosis My PR for local prettify config has been shipped already, please specify why the sample I posted is insufficient for you.