亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Undefined validation function 'bookabledate' on field 'CheckIn'

Undefined validation function 'bookabledate' on field 'CheckIn'

有沒有報這個錯的,win

正在回答

3 回答

最新版本,到底是要寫binding還是validate呢?gin框架文檔寫的binding,validator文檔寫的是validate

0 回復 有任何疑惑可以回復我~

試了很多版本都沒有成功,最后試了一下官網的實例:https://github.com/gin-gonic/gin#custom-validators

package?main

import?(
???"net/http"
???"time"

???"github.com/gin-gonic/gin"
???"github.com/gin-gonic/gin/binding"
???"github.com/go-playground/validator"
)

//?Booking?contains?binded?and?validated?data.
type?Booking?struct?{
???CheckIn??time.Time?`form:"check_in"?binding:"required,bookabledate"?time_format:"2006-01-02"`
???CheckOut?time.Time?`form:"check_out"?binding:"required,gtfield=CheckIn"?time_format:"2006-01-02"`
}

var?bookableDate?validator.Func?=?func(fl?validator.FieldLevel)?bool?{
???date,?ok?:=?fl.Field().Interface().(time.Time)
???if?ok?{
??????today?:=?time.Now()
??????if?today.After(date)?{
?????????return?false
??????}
???}
???return?true
}

func?main()?{
???route?:=?gin.Default()

???if?v,?ok?:=?binding.Validator.Engine().(*validator.Validate);?ok?{
??????v.RegisterValidation("bookabledate",?bookableDate)
???}

???route.GET("/bookable",?getBookable)
???route.Run(":8085")
}

func?getBookable(c?*gin.Context)?{
???var?b?Booking
???if?err?:=?c.ShouldBindWith(&b,?binding.Query);?err?==?nil?{
??????c.JSON(http.StatusOK,?gin.H{"message":?"Booking?dates?are?valid!"})
???}?else?{
??????c.JSON(http.StatusBadRequest,?gin.H{"error":?err.Error()})
???}
}

import 有一點改動,沒有加/v10,看了一下源文件是v10的,就去掉了

import?"github.com/go-playground/validator"

然后運行

$?curl?"http://127.0.0.1:8085/bookable?check_in=2021-04-02&check_out=2021-04-07"
{"message":"Booking?dates?are?valid!"}

$?curl?"http://127.0.0.1:8085/bookable?check_in=2021-04-09&check_out=2021-04-07"
{"error":"Key:?'Booking.CheckOut'?Error:Field?validation?for?'CheckOut'?failed?on?the?'gtfield'?tag"}

$?curl?"http://127.0.0.1:8085/bookable?check_in=2021-04-01&check_out=2021-04-07"
{"error":"Key:?'Booking.CheckIn'?Error:Field?validation?for?'CheckIn'?failed?on?the?'bookabledate'?tag"}%

能出來結果,可能是版之前的兼容性問題。先這么著吧。

0 回復 有任何疑惑可以回復我~
#1

errorgroup

bingding 改成 validate 就可以了,mac版本
2021-06-30 回復 有任何疑惑可以回復我~
#2

errorgroup 回復 errorgroup

但是規則驗證就失效了
2021-06-30 回復 有任何疑惑可以回復我~
type?Booking?struct

里把binding改成validate

0 回復 有任何疑惑可以回復我~
#1

日落梧桐 提問者

不是這個原因
2021-03-31 回復 有任何疑惑可以回復我~
#2

errorgroup 回復 日落梧桐 提問者

上面的程序在mac上,把binding改成validate就是可以的
2021-06-30 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Gin入門實戰
  • 參與學習       15951    人
  • 解答問題       55    個

會基礎就能上手golang web教程-Gin

進入課程

Undefined validation function 'bookabledate' on field 'CheckIn'

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號