validator icon indicating copy to clipboard operation
validator copied to clipboard

Is there's a way to custom error message for each field?

Open coolerfall opened this issue 5 years ago • 7 comments

Package version eg. v8, v9:

Issue, Question or Enhancement:

Code sample, to showcase or reproduce:

type User struct {
        Name     string `validate:"required, errMessage"`
	Password string
} 

I can get the errMessage in error when validate failed. PS: I don't need i18n support, just user friendly error message.

coolerfall avatar Jun 11 '19 02:06 coolerfall

@coolerfall Currently there is no way to pull the error message from the tags, and not sure if/when I might add that.

The error message itself has all the information surrounding the error and can be used to create your error message if not using the translations logic.

i18n support is provided, optionally, through registering translations. Currently, translations are using the following packages to provide i18n/l10n:

  • https://github.com/go-playground/universal-translator
  • https://github.com/go-playground/locales

deankarn avatar Jul 12 '19 15:07 deankarn

Thanks for sharing this. But I need to go through all the FieldErrors to convert to my custom message which seems not friendly/simple to use. It will be very nice if add this feature.

coolerfall avatar Jul 18 '19 01:07 coolerfall

Just adding to @coolerfall's suggestion.

Extracting error messages into a yaml file (or any other format file) will make like easier for users to customize their error messages.

One example is from the rails world: https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml

We could even interpolate using text/template package.

kulpreet avatar Feb 03 '20 10:02 kulpreet

Another option is to use govalidator's solution to the problem.

type User struct {
        Email     string `validate:"required~email is required,email"`
	Password string
} 

I find this solution really clean for the user. If we want more fine grain control we can use FieldErrors

threeaccents avatar Mar 01 '20 03:03 threeaccents

From now, is there any plans to implement this feature?

axetroy avatar Jun 04 '20 12:06 axetroy

@threeaccents what if you need english, russian and french languages?

frederikhors avatar Nov 29 '20 12:11 frederikhors

@threeaccents solution would be perfect for me, but unfortunately the govalidator library looks to be abandoned, whereas this package is still active.

In my case I want the validation errors to provide an error translation key. For example:

type User struct {
        Email     string `validate:"required~error-validation-email-invalid,email"`
	Password string
} 

Then our frontend team has a list of translation tables with mapping like:

en.json

    "error-validation-email-invalid": "Please check your email is set correctly",

Our reason for doing it this way is we'd like all translations to be held in one place, rather than a mismatch between some translations happening on the backend and others on the frontend.

OscarVanL avatar Aug 24 '22 10:08 OscarVanL