Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

验证失败时,没有显示struct tag自定义的消息 #160

Closed
inhere opened this issue Aug 24, 2022 · 1 comment
Closed

验证失败时,没有显示struct tag自定义的消息 #160

inhere opened this issue Aug 24, 2022 · 1 comment
Assignees
Labels
bug_fixed bug Something isn't working

Comments

@inhere
Copy link
Member

inhere commented Aug 24, 2022

验证失败时,为什么没有显示我自定义的消息。而是默认的英文消息(email value is invalid mail)

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/gookit/goutil/dump"
	"github.com/gookit/validate"
)

func main() {
	binding.Validator = &customValidator{}
	r := gin.Default()
	r.POST("/user/list", func(ctx *gin.Context) {
		var user User
		err := ctx.ShouldBindJSON(&user)
		if err != nil {
			dump.P(err)
			ctx.String(400, "出错"+err.Error())
			return
		}
		ctx.String(200, "验证通过!")
	})

	r.Run(":8080")
}

type User struct {
	Email string `json:"email" validate:"required|email" message:"required:eamil必填的信息|email:邮箱验证失败的信息"`
}

type customValidator struct{}

func (c *customValidator) ValidateStruct(ptr interface{}) error {
	v := validate.Struct(ptr)
	v.Validate() // do validating

	if v.Errors.Empty() {
		return nil
	}

	return v.Errors
}

func (c *customValidator) Engine() interface{} {
	return nil
}

Originally posted by @weiyunxiao in #156 (comment)

@inhere inhere changed the title > ![image](https://user-images.githubusercontent.com/5302062/186171801-e9237f60-5a65-4a93-8d22-8a4e49ecfcba.png) 验证失败时,没有显示struct tag自定义的消息 Aug 24, 2022
@inhere inhere self-assigned this Aug 24, 2022
@inhere inhere added the bug Something isn't working label Aug 24, 2022
@inhere inhere closed this as completed in 32f4d98 Aug 24, 2022
@inhere inhere reopened this Aug 24, 2022
@inhere
Copy link
Member Author

inhere commented Aug 24, 2022

@inhere inhere closed this as completed Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug_fixed bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant