在使用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中文网其他相关文章!