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

邮箱验证失败 #156

Closed
weiyunxiao opened this issue Aug 17, 2022 · 4 comments
Closed

邮箱验证失败 #156

weiyunxiao opened this issue Aug 17, 2022 · 4 comments
Assignees
Labels
question Further information is requested resolved

Comments

@weiyunxiao
Copy link

weiyunxiao commented Aug 17, 2022

用户邮箱地址是 xxxx.88@qq.com 时验证不通过

包是否可以使用如下规则
^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$

@inhere inhere added the enhancement New feature or request label Aug 17, 2022
@inhere
Copy link
Member

inhere commented Aug 23, 2022

image

我测试可以的呢?

@weiyunxiao
Copy link
Author

结构体tag试试

@weiyunxiao
Copy link
Author

image

我测试可以的呢?

现在是可以了。但又有个新问题。验证失败时,为什么没有显示我自定义的消息。而是默认的英文消息(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
}

@inhere
Copy link
Member

inhere commented Aug 24, 2022

提示消息这里有点问题,我开了一个新issue #160

@inhere inhere closed this as completed Aug 24, 2022
@inhere inhere added question Further information is requested resolved and removed enhancement New feature or request labels Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested resolved
Projects
None yet
Development

No branches or pull requests

2 participants