validate.js icon indicating copy to clipboard operation
validate.js copied to clipboard

Set attribute name

Open arb opened this issue 10 years ago • 17 comments

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.

arb avatar Aug 21 '15 14:08 arb

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.

ansman avatar Aug 24 '15 11:08 ansman

I think this is important too +1

bradwestfall avatar Mar 03 '16 23:03 bradwestfall

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.

uiii avatar Mar 09 '16 17:03 uiii

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

ansman avatar Mar 10 '16 09:03 ansman

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"
    }
)

uiii avatar Mar 10 '16 10:03 uiii

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.

michaek avatar Apr 07 '16 15:04 michaek

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

Saturate avatar Sep 20 '16 13:09 Saturate

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

vdh avatar Oct 20 '16 23:10 vdh

Any progress on this?

holm-dk avatar Apr 21 '17 17:04 holm-dk

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

vdh avatar Apr 22 '17 12:04 vdh

Mind if I bump this a little?

bump

mrchimp avatar Sep 06 '17 10:09 mrchimp

I'd also love to see this feature!

williamdwarke avatar Apr 20 '18 00:04 williamdwarke

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

vdh avatar Apr 20 '18 01:04 vdh

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 avatar Dec 18 '19 20:12 timmyomahony

@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);
  },
});

vdh avatar Dec 20 '19 01:12 vdh

@ansman what is the status?

mindaugasnakrosis avatar Aug 18 '20 13:08 mindaugasnakrosis

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

vdh avatar Aug 20 '20 23:08 vdh