202 Star 1.9K Fork 613

张奇峰 / GinSkeleton

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
show.go 1.43 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
张奇峰 提交于 2021-11-28 17:58 . 表单参数验证器测试通过
package users
import (
"github.com/gin-gonic/gin"
"goskeleton/app/global/consts"
"goskeleton/app/http/controller/web"
common_data_type "goskeleton/app/http/validator/common/data_type"
"goskeleton/app/http/validator/core/data_transfer"
"goskeleton/app/utils/response"
)
type Show struct {
// 表单参数验证结构体支持匿名结构体嵌套
UserName string `form:"user_name" json:"user_name" binding:"required,min=1"` // 必填、对于文本,表示它的长度>=1
common_data_type.Page
}
// 验证器语法,参见 Register.go文件,有详细说明
func (s Show) CheckParams(context *gin.Context) {
//1.基本的验证规则没有通过
if err := context.ShouldBind(&s); err != nil {
// 将表单参数验证器出现的错误直接交给错误翻译器统一处理即可
response.ValidatorError(context, err)
return
}
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
extraAddBindDataContext := data_transfer.DataAddContext(s, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
response.ErrorSystem(context, "UserShow表单验证器json化失败", "")
} else {
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
(&web.Users{}).Show(extraAddBindDataContext)
}
}
Go
1
https://gitee.com/daitougege/GinSkeleton.git
git@gitee.com:daitougege/GinSkeleton.git
daitougege
GinSkeleton
GinSkeleton
master

搜索帮助

14c37bed 8189591 565d56ea 8189591