在使用php中的Ozzo驗證v4時,可能會遇到一個常見的錯誤訊息:「ozzo 驗證 v4 傳回在結構中找不到欄位 #0」。這個錯誤提示表示驗證器在資料結構中無法找到指定的欄位。這可能是由於欄位名稱錯誤、資料結構不符或驗證規則設定錯誤等原因所引起的。在解決這個問題之前,我們需要仔細檢查程式碼和驗證規則,確保正確指定了欄位名稱並與資料結構相符。透過仔細排查和調試,我們可以輕鬆解決這個問題,讓程式碼順利運行。
我正在使用 "github.com/go-ozzo/ozzo-validation/v4"
。
這是我的結構:
type mystruct struct { uuid string `json:"uuid"` firstuuid string `json:"first_uuid"` seconduuid string `json:"second_uuid"` thirduuid string `json:"third_uuid"` phonenumber string `json:"phone_number"` email string `json:"email"` skypeid string `json:"skype_id"` city string `json:"city"` comment string `json:"comment"` personnelid string `json:"personnel_id"` firstdate string `json:"first_date"` seconddate string `json:"second_date"` firstboolean bool `json:"first_boolean"` secondboolean bool `json:"second_boolean"` }
這是驗證方法:
func (m mystruct) validate() error { err := validation.validatestruct( validation.field(&uui.personnelid, validation.match(personnelidregexp)), validation.field(&uui.uuid, is.uuid), validation.field(&uui.firstuuid, validation.required, is.uuid), validation.field(&uui.seconduuid, validation.required, is.uuid), validation.field(&uui.thirduuid, validation.required, is.uuid), validation.field(&uui.email, validation.required, is.email), validation.field(&uui.phonenumber, validation.required, validation.match(mobileregexp)), validation.field(&uui.city, validation.required), validation.field(&uui.comment), validation.field(&uui.skypeid, validation.required), validation.field(&uui.firstdate, validation.date(time.dateonly)), validation.field(&uui.seconddate, validation.date(time.dateonly)), ) return err }
這是我發送的請求:
{ "uuid": "1e57ef49-352f-4545-a43a-b51cad6c5a0a", "phone_number": "09124567891", "email": "[email protected]", "skype_id": "some_skype_id", "city": "a city", "personnel_id": "", "comment": "no comment for now!", "first_date": "", "second_date": "", "first_uuid": "94038913-2bdb-4dde-99fb-640a24e1c003", "second_uuid": "7fa0e242-841b-4de0-a3ce-e2b54ecd1bca", "third_uuid": "35ab6711-852e-42c8-aab3-dfb901a845f5", "first_boolean": true, "second_boolean": false }
我收到此錯誤:
field #0 cannot be found in the struct
validatestruct 的第一個參數是 structptr
:
func validatestruct(structptr interface{}, fields ...*fieldrules) error
您的程式碼中缺少此參數。不幸的是,編譯器無法捕獲此類錯誤。
這是修正後的實作(請注意,uui
也應該是 m
):
func (m MyStruct) Validate() error { err := validation.ValidateStruct( &m, // <== The first parameter is a pointer to the struct. validation.Field(&m.PersonnelID, validation.Match(personnelIDRegexp)), validation.Field(&m.UUID, is.UUID), validation.Field(&m.FirstUUID, validation.Required, is.UUID), validation.Field(&m.SecondUUID, validation.Required, is.UUID), validation.Field(&m.ThirdUUID, validation.Required, is.UUID), validation.Field(&m.Email, validation.Required, is.Email), validation.Field(&m.PhoneNumber, validation.Required, validation.Match(mobileRegexp)), validation.Field(&m.City, validation.Required), validation.Field(&m.Comment), validation.Field(&m.SkypeID, validation.Required), validation.Field(&m.FirstDate, validation.Date(time.DateOnly)), validation.Field(&m.SecondDate, validation.Date(time.DateOnly)), ) return err }
以上是ozzo 驗證 v4 傳回在結構中找不到欄位 #0的詳細內容。更多資訊請關注PHP中文網其他相關文章!