GoFrame框架數(shù)據(jù)校驗(yàn)之校驗(yàn)對象校驗(yàn)結(jié)構(gòu)體
前言摘要
這篇文章將會(huì)為大家介紹GoFrame數(shù)據(jù)校驗(yàn)中校驗(yàn)對象的知識(shí)點(diǎn),包括:Validator對象常用方法的介紹、單數(shù)據(jù)校驗(yàn)、校驗(yàn)Map、校驗(yàn)結(jié)構(gòu)體的示例。
基本概念
數(shù)據(jù)校驗(yàn)組件提供了數(shù)據(jù)校驗(yàn)對象:用于數(shù)據(jù)校驗(yàn)統(tǒng)一的配置管理,支持我們便捷的進(jìn)行鏈?zhǔn)讲僮鳌?/p>
方法介紹
type Validator func New() *Validator func (v *Validator) CheckMap(params interface{}) Error func (v *Validator) CheckStruct(object interface{}) Error func (v *Validator) CheckValue(value interface{}) Error func (v *Validator) Clone() *Validator func (v *Validator) Ctx(ctx context.Context) *Validator func (v *Validator) Data(data interface{}) *Validator func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator func (v *Validator) Messages(messages interface{}) *Validator func (v *Validator) Rules(rules interface{}) *Validator
簡要說明
- New()方法用于創(chuàng)建一個(gè)新的校驗(yàn)對象。
- CheckValue/CheckMap/CheckStruct方法用于特定參數(shù)類型的數(shù)據(jù)校驗(yàn),我們在項(xiàng)目開發(fā)中使用比較多的是CheckStruct,也建議大家使用CheckStruct。
- Ctx()方法用于傳遞Context上下文變量。
- I18n()方法用于設(shè)置當(dāng)前校驗(yàn)對象的I18N國際化組件,默認(rèn)情況下,校驗(yàn)組件使用的是框架全局默認(rèn)的i18n組件對象。
- Data()方法用于設(shè)置需要校驗(yàn)的數(shù)據(jù)集合,支持map類型或者struct類型。
- Rules()方法用于傳遞當(dāng)前鏈?zhǔn)讲僮餍r?yàn)的自定義校驗(yàn)規(guī)則,支持使用[]string類型或者map類型。
- Messages()方法用于傳遞當(dāng)前鏈?zhǔn)讲僮餍r?yàn)的自定義錯(cuò)誤提示信息,往往使用map類型傳遞,具體看后續(xù)代碼示例。
注意問題TIPS
在數(shù)據(jù)校驗(yàn)對象的CheckValue/CheckMap/CheckStruct方法中,不存在Context上下文變量參數(shù),而是通過鏈?zhǔn)讲僮鞯腃tx方法來控制。
GoFrame的g模塊中定義了Validator方法來快捷創(chuàng)建校驗(yàn)對象:官方也推薦我們使用g模塊的g.Validator()方式來快捷創(chuàng)建一個(gè)校驗(yàn)對象。
鏈?zhǔn)讲僮?/h2>
示例1:單數(shù)據(jù)校驗(yàn)
簡單示例
err := g.Validator().Rules("min:60").Messages("考試不及格").CheckValue(16) fmt.Println(err.String()) //打印結(jié)果:考試不及格
進(jìn)階示例
package main import ( "fmt" "github.com/gogf/gf/frame/g" ) func main() { data := g.Map{ "password": "123", //這個(gè)的作用僅是定義了這個(gè)結(jié)構(gòu)設(shè)置了默認(rèn)值,并不代表著傳入了值 } //Data()中的參數(shù)是需要進(jìn)行校驗(yàn)的數(shù)據(jù)集合,常用于map或者結(jié)構(gòu)體類型。 //CheckValue()是輸入的參數(shù) err := g.Validator().Data(data).Rules("required-with:password").Messages("請輸入確認(rèn)密碼").CheckValue("") if err != nil { fmt.Println("CheckValue傳入為空時(shí):" + err.String()) // 請輸入確認(rèn)密碼 } else { fmt.Println("CheckValue傳入為空時(shí):校驗(yàn)通過") } err = g.Validator().Data(data).Rules("required-with:password").Messages("請輸入確認(rèn)密碼").CheckValue("1") if err != nil { fmt.Println("CheckValue傳入不為空時(shí):" + err.String()) // 請輸入確認(rèn)密碼 } else { fmt.Println("CheckValue傳入不為空時(shí):校驗(yàn)通過") } }
進(jìn)階示例打印結(jié)果
示例2:Map數(shù)據(jù)校驗(yàn)
params := map[string]interface{}{ "passport": "", "password": "wangzhongyang", "password2": "zhongyang", } rules := map[string]string{ "passport": "required|length:6,16", "password": "required|length:6,16|same:password2", "password2": "required|length:6,16", } messages := map[string]interface{}{ "passport": "賬號(hào)不能為空|賬號(hào)長度應(yīng)當(dāng)在:min到:max之間", "password": map[string]string{ "required": "密碼不能為空", "same": "兩次密碼輸入不相等", }, } err := g.Validator().Messages(messages).Rules(rules).CheckMap(params) if err != nil { g.Dump(err.Maps()) }
執(zhí)行后,終端輸出:
{ "passport": { "length": "賬號(hào)長度應(yīng)當(dāng)在6到16之間", "required": "賬號(hào)不能為空" }, "password": { "same": "兩次密碼輸入不相等" } }
示例3:Struct數(shù)據(jù)校驗(yàn)
type User struct { Name string `v:"required#請輸入用戶姓名"` Type int `v:"required#請選擇用戶類型"` } data := g.Map{ "name": "wangzhongyang", } user := User{} if err := gconv.Scan(data, &user); err != nil { panic(err) } err := g.Validator().Data(data).CheckStruct(user) if err != nil { fmt.Println(err.Items()) //[map[Type:map[required:請選擇用戶類型]]] }
總結(jié)
這篇文章為大家介紹了GoFrame數(shù)據(jù)校驗(yàn)之校驗(yàn)對象的知識(shí)點(diǎn),包括:Validator對象常用方法的介紹、單數(shù)據(jù)校驗(yàn)、校驗(yàn)Map、校驗(yàn)結(jié)構(gòu)體的示例。
更多關(guān)于GoFrame校驗(yàn)結(jié)構(gòu)體的資料請關(guān)注腳本之家其它相關(guān)文章!
- GoFrame框架使用避坑指南和實(shí)踐干貨
- GoFrame?gmap遍歷hashmap?listmap?treemap使用技巧
- GoFrame?gredis配置文件及配置方法對比
- 適合PHP同學(xué)的GoFrame框架使用體驗(yàn)及學(xué)習(xí)建議
- GoFrame?ORM原生方法操作示例
- GoFrame 框架緩存查詢結(jié)果的示例詳解
- GoFrame錯(cuò)誤處理常用方法及錯(cuò)誤碼使用示例
- GoFrame框架Scan類型轉(zhuǎn)換實(shí)例
- GoFrame通用類型變量gvar與interface基本使用對比
- GoLang編程必備:GoFrame?GoLand插件介紹
相關(guān)文章
go語言實(shí)現(xiàn)的memcache協(xié)議服務(wù)的方法
這篇文章主要介紹了go語言實(shí)現(xiàn)的memcache協(xié)議服務(wù)的方法,實(shí)例分析了Go語言使用memcache的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03gin自定義中間件解決requestBody不可重復(fù)讀問題(最新推薦)
這篇文章主要介紹了gin自定義中間件解決requestBody不可重復(fù)讀問題,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Golang中生成隨機(jī)字符串并復(fù)制到粘貼板的方法
這篇文章主要介紹了Golang中生成隨機(jī)字符串并復(fù)制到粘貼板的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Golang實(shí)現(xiàn)Biginteger大數(shù)計(jì)算實(shí)例詳解
這篇文章主要為大家介紹了Golang實(shí)現(xiàn)Biginteger大數(shù)計(jì)算實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07golang實(shí)現(xiàn)ping命令的完整代碼
這篇文章給大家介紹了如何使用golang實(shí)現(xiàn)ping命令,文中給大家介紹了完整的實(shí)現(xiàn)代碼,并有詳細(xì)的圖文講解,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02