gin表单验证显示中文
概述
使用反射发的方式
简单使用
package main
import (
"fmt"
"gopkg.in/go-playground/validator.v9"
)
// 安装 go get gopkg.in/go-playground/validator.v9
type InputLogin struct {
Email string `validate:"required,email"`
Password string `validate:"required,max=16,min=6"`
Code int `validate:"gte=1000,lte=9999"`
}
func main() {
v := validator.New()
input := &InputLogin{
Email: "jet@23.com",
Password: "123456",
Code: 4444,
}
err := v.Struct(input)
if err != nil {
for _, err := range err.(validator.ValidationErrors) {
fmt.Println(err)
}
return
}
fmt.Println("ok")
}
- 提示消息英文
- 提示消息是固定的
翻译
package main
import (
"fmt"
"reflect"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"gopkg.in/go-playground/validator.v9"
zhTranslations "gopkg.in/go-playground/validator.v9/translations/zh"
)
// 安装 go get gopkg.in/go-playground/validator.v9
type InputLogin struct {
Email string `json:"email" validate:"required,email" label:"邮箱"`
Password string `json:"password" validate:"required,max=16,min=6" label:"密码"`
Code int `json:"code" validate:"gte=1000,lte=9999" label:"验证码"`
C1 string `json:"c1" validate:"checkName" label:"测试字段"`
}
func checkName(fl validator.FieldLevel) bool {
val := fl.Field().String()
l1 := fl.GetTag()
fmt.Println(l1)
l2 := fl.Param()
fmt.Println(l2)
fmt.Println("----->", val)
//如果是张三就会触发验证
return val != "张三"
}
func main() {
input := &InputLogin{
Email: "jet@dsd.com",
Password: "12356",
Code: 444411,
C1: "张三",
}
//中文翻译器
zh_ch := zh.New()
uni := ut.New(zh_ch)
trans, _ := uni.GetTranslator("zh")
v := validator.New()
//注册自定义验证函数
v.RegisterValidation("checkName", checkName)
//注册翻译器
zhTranslations.RegisterDefaultTranslations(v, trans)
// 自定义错误信息
v.RegisterTranslation("checkName", trans, func(ut ut.Translator) error {
return ut.Add("checkName", "{0}字段名称不能叫张三!", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("checkName", fe.Field())
return t
})
v.RegisterTagNameFunc(func(field reflect.StructField) string {
fmt.Println("----->", field.Name)
//通过反射取出label
label := field.Tag.Get("label")
//fmt.Sprint
return fmt.Sprint(field.Name, ",", label)
})
err := v.Struct(input)
if err != nil {
for _, err := range err.(validator.ValidationErrors) {
//翻译输出
m := err.Translate(trans)
fmt.Println(m)
}
return
}
fmt.Println("ok")
}
打印结果为
Password,密码长度必须至少为6个字符
Code,验证码必须小于或等于9,999
C1,测试字段字段名称不能叫张三!