mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
add v tag for gvalid; add p tag for ghttp; add c tag for gconv
This commit is contained in:
@ -8,5 +8,5 @@
|
||||
package ghttp
|
||||
|
||||
var (
|
||||
paramTagPriority = []string{"param", "params"}
|
||||
paramTagPriority = []string{"param", "params", "p"}
|
||||
)
|
||||
|
||||
@ -40,7 +40,7 @@ var (
|
||||
}
|
||||
|
||||
// Priority tags for Map*/Struct* functions.
|
||||
structTagPriority = []string{gGCONV_TAG, "json"}
|
||||
structTagPriority = []string{gGCONV_TAG, "c", "json"}
|
||||
)
|
||||
|
||||
// Convert converts the variable <i> to the type <t>, the type <t> is specified by string.
|
||||
|
||||
@ -15,8 +15,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// 同时支持valid和gvalid标签,优先使用valid
|
||||
structTagPriority = []string{"valid", "gvalid"}
|
||||
// 同时支持gvalid、valid和v标签,优先使用gvalid
|
||||
structTagPriority = []string{"gvalid", "valid", "v"}
|
||||
)
|
||||
|
||||
// 校验struct对象属性,object参数也可以是一个指向对象的指针,返回值同CheckMap方法。
|
||||
|
||||
@ -1,19 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gbase64"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/util/gvalid"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data := "HwHsGhXMaGc==="
|
||||
datab, err := gbase64.Decode([]byte(data))
|
||||
fmt.Println(err)
|
||||
fmt.Println(datab)
|
||||
fmt.Println(string(datab))
|
||||
|
||||
s, e := base64.StdEncoding.DecodeString(data)
|
||||
fmt.Println(e)
|
||||
fmt.Println(string(s))
|
||||
type User struct {
|
||||
Uid int `v:"uid @integer|min:1"`
|
||||
Name string `v:"name @required|length:6,30#请输入用户名称|用户名称长度非法"`
|
||||
Pass1 string `v:"password1@required|password3"`
|
||||
Pass2 string `v:"password2@required|password3|same:password1#||两次密码不一致,请重新输入"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
user := &User{
|
||||
Name: "john",
|
||||
Pass1: "Abc123!@#",
|
||||
Pass2: "123",
|
||||
}
|
||||
|
||||
// 使用结构体定义的校验规则和错误提示进行校验
|
||||
if e := gvalid.CheckStruct(user, nil); e != nil {
|
||||
g.Dump(e.Maps())
|
||||
}
|
||||
|
||||
// 自定义校验规则和错误提示,对定义的特定校验规则和错误提示进行覆盖
|
||||
rules := map[string]string{
|
||||
"uid": "min:6",
|
||||
}
|
||||
msgs := map[string]interface{}{
|
||||
"password2": map[string]string{
|
||||
"password3": "名称不能为空",
|
||||
},
|
||||
}
|
||||
if e := gvalid.CheckStruct(user, rules, msgs); e != nil {
|
||||
g.Dump(e.Maps())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user