Table of Contents
Question content
Solution
Home Backend Development Golang ozzo validation v4 returns field #0 not found in structure

ozzo validation v4 returns field #0 not found in structure

Feb 11, 2024 am 11:00 AM

ozzo 验证 v4 返回在结构中找不到字段 #0

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.

Question content

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"`
}
Copy after login

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
}
Copy after login

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
}
Copy after login

I get this error:

field #0 cannot be found in the struct
Copy after login

Solution

validatestruct The first parameter is structptr:

func validatestruct(structptr interface{}, fields ...*fieldrules) error
Copy after login

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
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

How do you use the pprof tool to analyze Go performance? How do you use the pprof tool to analyze Go performance? Mar 21, 2025 pm 06:37 PM

The article explains how to use the pprof tool for analyzing Go performance, including enabling profiling, collecting data, and identifying common bottlenecks like CPU and memory issues.Character count: 159

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

How do you write unit tests in Go? How do you write unit tests in Go? Mar 21, 2025 pm 06:34 PM

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

How do you specify dependencies in your go.mod file? How do you specify dependencies in your go.mod file? Mar 27, 2025 pm 07:14 PM

The article discusses managing Go module dependencies via go.mod, covering specification, updates, and conflict resolution. It emphasizes best practices like semantic versioning and regular updates.

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

See all articles