Skip to content

Commit 9729063

Browse files
committed
add validation example
1 parent db6ce13 commit 9729063

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

httpserver/xsrf/main.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
package main
1616

1717
import (
18-
"html/template"
19-
2018
"github.com/beego/beego/v2/server/web"
19+
"html/template"
2120
)
2221

2322
func main() {
@@ -28,6 +27,7 @@ func main() {
2827
mc := &MainController{}
2928

3029
web.Router("/xsrfpage", mc, "get:XsrfPage")
30+
web.Router("/xsrfjson", mc, "get:XsrfJSON")
3131
web.Router("/new_message", mc, "post:NewMessage")
3232

3333
web.Run(":8080")
@@ -43,7 +43,18 @@ func (mc *MainController) XsrfPage() {
4343
mc.TplName = "xsrf.html"
4444
}
4545

46+
func (mc *MainController) XsrfJSON() {
47+
mc.XSRFExpire = 7200
48+
type data struct {
49+
XsrfToken string `json:"xsrfToken"`
50+
}
51+
token := mc.XSRFToken()
52+
mc.Ctx.Output.Header("xsrf", token)
53+
_ = mc.JSONResp(&data{XsrfToken: token})
54+
}
55+
4656
func (mc *MainController) NewMessage() {
57+
mc.CheckXSRFCookie()
4758
v, _ := mc.Input()
4859
mc.Ctx.WriteString("hello" + v.Get("message"))
4960
}

validation/main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"strings"
6+
7+
"github.com/beego/beego/v2/core/validation"
8+
)
9+
10+
// Set validation function in "valid" tag
11+
// Use ";" as the separator of multiple functions. Spaces accept after ";"
12+
// Wrap parameters with "()" and separate parameter with ",". Spaces accept after ","
13+
// Wrap regex match with "//"
14+
type user struct {
15+
Id int
16+
Name string `valid:"Required;Match(/^Bee.*/)"` // Name cannot be empty and must have prefix Bee
17+
Age int `valid:"Range(1, 140)"` // 1 <= Age <= 140
18+
Email string `valid:"Email; MaxSize(100)"` // Email must be valid email address and the length must be <= 100
19+
Mobile string `valid:"Mobile"` // Mobile must be valid mobile
20+
IP string `valid:"IP"` // IP must be a valid IPV4 address
21+
Address string `valid:"ChinaAddress"`
22+
}
23+
24+
func (u *user) Valid(v *validation.Validation) {
25+
if strings.Index(u.Name, "admin") != -1 {
26+
// 通过 SetError 设置 Name 的错误信息,HasErrors 将会返回 true
27+
v.SetError("Name", "name should contain word admin")
28+
}
29+
}
30+
31+
func main() {
32+
_ = validation.AddCustomFunc("ChinaAddress", func(v *validation.Validation, obj interface{}, key string) {
33+
addr, ok := obj.(string)
34+
if !ok {
35+
return
36+
}
37+
if !strings.HasPrefix(addr, "China") {
38+
v.AddError(key, "China address only")
39+
}
40+
})
41+
valid := validation.Validation{}
42+
u := user{Name: "Beego", Age: 2, Email: "[email protected]"}
43+
b, err := valid.Valid(&u)
44+
if err != nil {
45+
// handle error
46+
}
47+
if !b {
48+
// validation does not pass
49+
// blabla...
50+
for _, err := range valid.Errors {
51+
log.Println(err.Key, err.Message)
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)