Skip to content

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

Closed
mytc opened this issue Sep 29, 2020 · 7 comments
Closed

Bind formdata with array input. #1644

mytc opened this issue Sep 29, 2020 · 7 comments

Comments

@mytc
Copy link

mytc commented Sep 29, 2020

I having having problem with binding the form data.
The input would be like this

<input name="codes[1][icode]" type="number" />
<input name="codes[1][limit]" type="text" />
<input name="codes[2][icode]" type="number" />
<input name="codes[2][limit]" type="text" />

And the struct

type Event struct {
	gorm.Model
	Code []InvideCode `form:"codes"`
}


type InvideCode struct {
	gorm.Model
	EventID uint 
	Code string `form:"icode"`
	Limit string `form:"limit"`
}

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.

@mytc mytc closed this as completed Sep 30, 2020
@mytc mytc reopened this Sep 30, 2020
@mytc mytc changed the title Bind Bind formdata with array input. Sep 30, 2020
@stale
Copy link

stale bot commented Dec 25, 2020

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.

@stale stale bot added the stale Marked as stale for auto-closing label Dec 25, 2020
@lammel
Copy link
Contributor

lammel commented Mar 9, 2021

Data binding was improved with Echo v4.2. Please check the docs for request binding
Please close if this is resolved for you @mytc

@kidlj
Copy link

kidlj commented Jun 17, 2022

The latest several versions of Echo supports this behavior like the following:

type Event struct {
	gorm.Model
	Code []InvideCode `form:"codes[]"`
}

@stale stale bot removed the stale Marked as stale for auto-closing label Jun 17, 2022
@dovoselly
Copy link

The latest several versions of Echo supports this behavior like the following:

type Event struct {
	gorm.Model
	Code []InvideCode `form:"codes[]"`
}

It returned an empty array

@kidlj
Copy link

kidlj commented Jun 17, 2022

The latest several versions of Echo supports this behavior like the following:

type Event struct {
	gorm.Model
	Code []InvideCode `form:"codes[]"`
}

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.

@aldas
Copy link
Contributor

aldas commented Jun 17, 2022

Echo supports binding data to slices when form value is named codes[] but not codes[N] where N is "dynamic" in form value name.

When you post

<input name="codes[1][icode]" type="number" />
<input name="codes[1][limit]" type="text" />
<input name="codes[2][icode]" type="number" />
<input name="codes[2][limit]" type="text" />

What Echo sees from Request is 4 different form values with following names:

  • codes[1][icode]
  • codes[1][limit]
  • codes[2][icode]
  • codes[2][limit]

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)

@Septrum101
Copy link

you can use
https://pkg.go.dev/github.com/go-playground/form/[email protected]#Decoder.Decode
to bind data to struct not map. It seems that there is no magic like laravel to parse formdata.

@lammel lammel closed this as completed Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants