kataras / iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:
https://www.iris-go.com
BSD 3-Clause "New" or "Revised" License
25.11k stars 2.48k forks source link

[BUG] schema: invalid path "username" #2456

Open baiyuxiong opened 3 months ago

baiyuxiong commented 3 months ago

Describe the bug Browser shows: schema: invalid path "username"schema: invalid path "username"

To Reproduce Steps to reproduce the behavior: codes as follow:

//main.go
user := mvc.New(app.Party("/my"))
    user.Register(
        userService,
        sessManager.Start,
    )
    user.Handle(new(controller.MyController))

//my_controller.go
type MyController struct {
    Ctx     iris.Context
    Service service.UserService
    Session *sessions.Session
}

func (c *MyController) GetLogin() mvc.Result {
    return mvc.View{
        Name: "my/login.html",
    }
}

type loginForm struct {
    Username string `form:"username"`
}

func (c *MyController) PostLogin(form loginForm) mvc.Result {
    fmt.Println(form.Username)
    return mvc.Response{
        Path: "/my/login",
    }
}

//my/login.html
<form action="/my/login" method="POST" enctype="multipart/form-data">
    <div class="container">
        <label><b>Username</b></label>
        <input type="text" placeholder="Enter Username" name="username" required>

        <button type="submit">Login</button>
    </div>
</form>

After click the login button, the page shows : schema: invalid path "username"schema: invalid path "username"

Desktop (please complete the following information):

iris.Version

baiyuxiong commented 3 months ago

I have found the reason why. It is due to my implementation of the NewUserService function.

userService in main.go is created by : userService := service.NewUserService()

This NewUserService is wrong:

func NewUserService() *UserService {
    return &UserService{}
}

and if * was removed, it will work fine:

func NewUserService() UserService {
    return UserService{}
}

Superficially,the error msg "schema: invalid path" has nothing to do NewUserService. It makes me so confused.