-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
Question Description
I have a field in my form, its name is the description
Example:
<input name="item" value="item1">
<input name="description" value="description1, this is a description for item one, the first item is very nice, bla bla bla">
<input name="item" value="item2">
<input name="description" value="description2">
<input name="item" value="item3">
<input name="description" value="description3">
On my server side, I define a struct with the information:
FormCreate struct {
Item []string `json:"item" form:"item"`
Description []string `json:"description" form:"description"`
}
formData = new(FormCreate)
c.Bind().Body(formData)
I have a problem with binding data from the form to my formData.
My expectation data for formData.Description is []string{"description, this is a description for item one, the first item is very nice, bla bla bla", "description2", "description3"} (This has 3 values for the slice).
But currently, I received for formData.Description after binding data is []string{"description1", "this is a description for item one", "the first item is very nice", "bla bla bla", "description2", "description3"} (This has 6 values for the slice).
It seems that the Bind() function has split the value of the description field of the first item into a slice by the "," , so I received the data for formData.Description is []string{"description1", "this is a description for item one", "the first item is very nice", "bla bla bla", "description2", "description3"}.
Who can resolve my problem?
Thank you so much!
Code Snippet (optional)
package main
import "github.com/gofiber/fiber/v3"
import "log"
func main() {
app := fiber.New()
// An example to describe the question
app.Post("/create-post", handleCreate)
log.Fatal(app.Listen(":3000"))
}
function handleCreate(c fiber.Ctx) error{
FormCreate struct {
Item []string `json:"item" form:"item"`
Description []string `json:"description" form:"description"`
}
var (
formData = new(FormCreate)
)
if c.Method() == http.MethodPost {
err = c.Bind().Body(formData)
}
return c.Render("index", fiber.Map{
"formData": formData,
})
}Checklist:
- I agree to follow Fiber's Code of Conduct.
- I have checked for existing issues that describe my questions prior to opening this one.
- I understand that improperly formatted questions may be closed without explanation.