gin bind(重要)
文章目录
- 一、数据绑定:
- 二、数据验证:
-
- 1. 使用:
- 2. 其他验证器:
- 3. 自定义验证器:
- 3.2 定义验证器:
- 3.3 注册验证器:
- 3.4 结构体中使用:
- 4. beego中的验证器:
- 4.2 验证方法:
- 4.3 通过 StructTag校验数据:
-
- BindQuery\ShouldBindQuery函数只绑定查询参数,而不绑定POST数据
- Bind\ShouldBind函数绑定Get 参数,绑定POST数据
一、数据绑定:
1. 数据绑定介绍:
Gin提供了两类绑定方法:
- Must bind:
- Methods:
Bind, BindJSON, BindXML, BindQuery, BindYAML
- Behavior:
- 这些方法属于MustBindWith的具体调用. 如果发生绑定错误, 则请求终止, 并触发
c.AbortWithError(400, err).SetType(ErrorTypeBind)
响应状态码被设置为 400 并且Content-Type被设置为text/plain; charset=utf-8
. 如果您在此之后尝试设置响应状态码, Gin会输出日志[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 422
. 如果您希望更好地控制绑定, 考虑使用ShouldBind等效方法.
- 这些方法属于MustBindWith的具体调用. 如果发生绑定错误, 则请求终止, 并触发
- Methods:
- Should bind:
- Methods:
ShouldBind, ShouldBindJSON, ShouldBindXML, ShouldBindQuery, ShouldBindYAML
- Behavior:
- 这些方法属于ShouldBindWith的具体调用. 如果发生绑定错误, Gin 会返回错误并由开发者处理错误和请求.
- Methods:
2. 数据绑定–Should bind:
2.1 ShouldBind:
.
├── chapter05
│ └── bind_form.go
├── main.go
├── static
├── template
│ ├── chapter05
│ │ └── user_add.html
./main.go
package main
import (
"gin_project/chapter05"
"github.com/gin-gonic/gin"
)
func main() {
engine := gin.Default()
// 注册模板
engine.LoadHTMLGlob("template/**/*")
// 注册静态文件
engine.Static("./static", "static")
// 注册路由
engine.GET("/to_bindform", chapter05.ToBindform)
engine.POST("/push_bindform", chapter05.DoBindform)
engine.GET("/push_bindquery", chapter05.DoBindquery) // http://localhost:9000/push_bindquery?name=zhangsan&age=110&addr=%E5%8C%97%E4%BA%AC
engine.POST("/push_bindajax", chapter05.DoBindajax)
engine.Run(":9000")
}
- ./chapter05/bind_form.go
package chapter05
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
type User struct {
Name string `form:"name" json:"name"`
Age int `form:"age" json:"age"`
Addr string `form:"addr" json:"addr"`
}
func ToBindform(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "chapter05/user_add.html", nil)
}
// bind form data
func DoBindform(ctx *gin.Context) {
var user User
err := ctx.ShouldBind(&user)
fmt.Println(user)
if err != nil {
ctx.String(http.StatusNotFound, "绑定form失败")
} else {
ctx.String(http.StatusOK, "绑定form成功")
}
}
// bind Query data
func DoBindquery(ctx *gin.Context) {
var user User
err := ctx.ShouldBind(&user)
fmt.Println(user)
if err != nil {
ctx.String(http.StatusNotFound, "绑定query失败")
} else {
ctx.String(http.StatusOK, "绑定query成功")
}
}
// bind Ajax data
func DoBindajax(ctx *gin.Context) {
var user User
err := ctx.ShouldBind(&user)
fmt.Println(user)
if err != nil {
ctx.String(http.StatusNotFound, "绑定ajax失败")
} else {
ctx.String(http.StatusOK, "绑定ajax成功")
}
}
./template/chapter05/user_add.html
{{ define "chapter05/user_add.html" }}
<!DOCTYPE html>
<html lang="zh">
<head>
<title>post请求练习</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style>
.userForm {
width: 480px;
height: 360px;
margin: 20px 200px;
}
input {
margin: 5px 0;
}
</style>
</head>
<body>
<div class="userForm">
<h2>bind form data</h2>
<form action="/push_bindform" method="post">
<p>姓名:<input type="text" name="name" id="name"></p>
<p>年龄:<input type="text" name="age" id="age"></p>
<p>地址:<input type="text" name="addr" id="addr"></p>
<p><input type="submit" value="提交form"></p>
<p><input type="button" value="提交ajax" id="sub-btn"></p>
</form>
</div>
<script>