-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Bind formdata with array input. #1644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed within a month if no further activity occurs. Thank you for your contributions. |
Data binding was improved with Echo v4.2. Please check the docs for request binding |
The latest several versions of Echo supports this behavior like the following:
|
It returned an empty array |
I haven't read the source code of Echo for this part, but it works for my slightly different cases: <form>
<div class="items">
{{range .Names}}
<input type="checkbox" id="{{.ID}}" name="ids[]" value={{.ID}} hidden>
<label for="{{.ID}}">{{.Name}}</label>
{{end}}
</div>
</form> With this handler: type examPayload struct {
IDs []int `form:"ids[]"`
}
func (h *Handler) createExam(c echo.Context) error {
payload := new(examPayload)
err := c.Bind(payload)
if err != nil {
return echo.ErrBadRequest
}
if len(payload.IDs) == 0 {
return echo.ErrBadRequest
}
u, err := h.userService.GetUserFromContext(c)
if err != nil {
return echo.ErrUnauthorized
}
exam, err := h.service.CreateExam(c.Request().Context(), u.ID, payload.IDs)
if err != nil {
return echo.ErrInternalServerError
}
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/exam?id=%d", exam.ID))
} I didn't test for op's case, sorry. |
Echo supports binding data to slices when form value is named When you post
What Echo sees from Request is 4 different form values with following names:
There is no (currently) built in magic to guess from brackets and number if it is suppose to be array of things. I am quite sure there are plenty of libraries that can extract form values from Go standard library Request struct like that (Echo is built on Go standard library Request struct) |
you can use |
I having having problem with binding the form data.
The input would be like this
And the struct
And i bind it with
event := new(Event)
or
c.FormValue("codes")
Both give me the empty value.
What did I done wrong?
Actually there is similar problem was asked at SO , the accepted answer is using 3rd party library and I don't like it. I wonder if there any Echo way to do it.
The text was updated successfully, but these errors were encountered: