Skip to content
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

Bind a checkbox group #129

Closed
phrozen opened this issue Sep 30, 2014 · 7 comments
Closed

Bind a checkbox group #129

phrozen opened this issue Sep 30, 2014 · 7 comments
Labels

Comments

@phrozen
Copy link

phrozen commented Sep 30, 2014

Hey I have the following code:

<input type="checkbox" name="colors" value="red">Red&nbsp;
<input type="checkbox" name="colors" value="green">Green&nbsp;
<input type="checkbox" name="colors" value="blue">Blue&nbsp;
<input type="checkbox" name="colors" value="black">Black&nbsp;
<input type="checkbox" name="colors" value="white">White&nbsp;

What is the correct way to bind them (using Bind Middleware) to obtain all data from them? I'm binding to an string but I only get the first one, if I bind to a slice I get an error.

Any ideas? Does Bind support this?

This is done in other languages by naming the group as an array:

<input type="checkbox" name="colors[]" value="purple">Purple&nbsp;

Then my struct will have a slice to bind. But I can't seem to make it work here.

@techjanitor
Copy link
Contributor

techjanitor commented Oct 2, 2014

Hmm I haven't tested it, but in Gorilla that would work like this:

type Form struct {
 Colors []Color
}

and the form would look like this:

<input type="checkbox" name="Colors.0.Color" value="red">
<input type="checkbox" name="Colors.1.Color" value="green">
<input type="checkbox" name="Colors.2.Color" value="blue">
<input type="checkbox" name="Colors.3.Color" value="black">
<input type="checkbox" name="Colors.4.Color" value="white">

@phrozen
Copy link
Author

phrozen commented Oct 2, 2014

I'm reading the code on Binding and AFAIK is not supported. Apparently slices are supported but not the way checkboxes work, just with json or xml body. Maybe I will start using Gorilla for that.

@EtienneR
Copy link

EtienneR commented Jun 4, 2015

Use this HTML syntax :

<input type="checkbox" name="colors[]" value="purple" id="purple">
<label for="purple">Purple</label>

In your Go structure, defined a string array for your checkboxes :

type Form struct {
 Colors []string `form:"colors[]"`
}

Your result will be an array like this :

[purple red green blue black white]

@manucorporat
Copy link
Contributor

@EtienneR does that really works? it would be interesting to add a unit test for that!

@EtienneR
Copy link

Yes, it's works for me (with the v1.0rc2 of Gin) :
In the main.go :

package main

import (
    "github.com/gin-gonic/gin"
)

type myForm struct {
    Colors []string `form:"colors[]"`
}

func main() {
    r := gin.Default()

    r.LoadHTMLGlob("views/*")
    r.GET("/", indexHandler)
    r.POST("/", formHandler)

    r.Run(":8080")
}

func indexHandler(c *gin.Context) {
    c.HTML(200, "form.html", nil)
}

func formHandler(c *gin.Context) {
    var fakeForm myForm
    c.Bind(&fakeForm)
    c.JSON(200, gin.H{"color": fakeForm.Colors})
}

And the form ("views/form.html") :

<form action="/" method="POST">
    <p>Check some colors</p>
    <label for="red">Red</label>
    <input type="checkbox" name="colors[]" value="red" id="red" />
    <label for="green">Green</label>
    <input type="checkbox" name="colors[]" value="green" id="green" />
    <label for="blue">Blue</label>
    <input type="checkbox" name="colors[]" value="blue" id="blue" />
    <input type="submit" />
</form>

If we check all colors, the result is like :

{"color":["red","green","blue"]}

@mh-cbon
Copy link

mh-cbon commented Aug 24, 2016

@manucorporat anyways to update the binding section of the doc ? EtienneR's solution works for me too.

@croatiangrn
Copy link

croatiangrn commented Jul 9, 2018

Is there a way to bind a slice of structs like this using formData in JS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants