validator icon indicating copy to clipboard operation
validator copied to clipboard

Fields not execute `RegisterTagNameFunc`

Open axetroy opened this issue 5 years ago • 2 comments

Package version eg. v8, v9:

v10

Issue, Question or Enhancement:

I use RegisterTagNameFunc to customize the name of the field

But when I use only validators such as eqfield nefield, it does not execute RegisterTagNameFunc() to get the name of the field

Code sample, to showcase or reproduce:

	validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
		return fld.Tag.Get("comment")
	})
type SetPayPasswordParams struct {
	Password        string `json:"password" validate:"required,numeric,len=6" comment:"密码"`
	PasswordConfirm string `json:"password_confirm" validate:"required,numeric,len=6,eqfield=Password" comment:"确认密码"`
}

Visible by the above tag

PasswordConfirm needs to be equal with Passowrd

If not equal, It should throw an exception 确认密码必须等于密码, But actually, got this 确认密码必须等于Password

- 确认密码必须等于密码
+ 确认密码必须等于Password

axetroy avatar Jun 05 '20 03:06 axetroy

err_key := []string{"Password","密码"}

 for _, e := range errs {

  re := strings.NewReplacer(err_key...)

  content := re.Replace(e.Translate(trans))

  errList = append(errList, content)

}

只能治标。。。

cleveng avatar Jan 29 '21 06:01 cleveng

It's very common in our projects, that the backend should return the map of the errors which is keyed by the field name of the form. Usually not the Go struct key, but the lowercase one in the form(from frontend). So that, the frontend can aware the error and display it by it's requirement.

zhangv avatar Oct 21 '21 06:10 zhangv