When using Ozzo validation v4 in PHP, you may encounter a common error message: "ozzo validation v4 returned field #0 not found in structure". This error message indicates that the validator cannot find the specified field in the data structure. This may be caused by incorrect field names, mismatched data structures, or incorrectly set validation rules. Before solving this problem, we need to double-check the code and validation rules to ensure that the field names are specified correctly and match the data structure. With careful troubleshooting and debugging, we can easily resolve this issue and get the code running smoothly.
I am using "github.com/go-ozzo/ozzo-validation/v4"
.
This is my structure:
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"` }
This is the verification method:
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 }
This is the request I sent:
{ "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 }
I get this error:
field #0 cannot be found in the struct
validatestruct The first parameter is structptr
:
func validatestruct(structptr interface{}, fields ...*fieldrules) error
This parameter is missing from your code. Unfortunately, the compiler cannot catch such errors.
This is the corrected implementation (note that uui
should also be 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 }
The above is the detailed content of ozzo validation v4 returns field #0 not found in structure. For more information, please follow other related articles on the PHP Chinese website!