diff --git a/g/net/ghttp/ghttp.go b/g/net/ghttp/ghttp.go index 53930655b..f99b14c73 100644 --- a/g/net/ghttp/ghttp.go +++ b/g/net/ghttp/ghttp.go @@ -8,5 +8,5 @@ package ghttp var ( - paramTagPriority = []string{"param", "params"} + paramTagPriority = []string{"param", "params", "p"} ) diff --git a/g/util/gconv/gconv.go b/g/util/gconv/gconv.go index 15007083e..22eec3a76 100644 --- a/g/util/gconv/gconv.go +++ b/g/util/gconv/gconv.go @@ -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 to the type , the type is specified by string. diff --git a/g/util/gvalid/gvalid_check_struct.go b/g/util/gvalid/gvalid_check_struct.go index fba014f95..d22768a87 100644 --- a/g/util/gvalid/gvalid_check_struct.go +++ b/g/util/gvalid/gvalid_check_struct.go @@ -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方法。 diff --git a/geg/other/test.go b/geg/other/test.go index 381a581b9..9b345d623 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -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()) + } }