monoculum / formam

a package for decode form's values into struct in Go
Apache License 2.0
190 stars 19 forks source link

parse multiselect? #3

Closed xpbliss closed 8 years ago

xpbliss commented 8 years ago

For this example, I'm unable to use a multiselect. I have not tried any other element such as checkbox or file. This is a huge show-stopper for me.

<select multiple="multiple" name="mydata[]">

Or

<select multiple="multiple" name="mydata">

Struct:

type MyDataStruct struct {
    Data    []string   `formam:"mydata"`
}
emilgpa commented 8 years ago

formam needs that you indicate the index of array, but I will solved this problem in a few hours for that you not need indicate the index

emilgpa commented 8 years ago

Thanks!

xpbliss commented 8 years ago

the issue is still.

emilgpa commented 8 years ago

Do you updated it? If the answer is yes... Do you can pass me the code that you is using?

xpbliss commented 8 years ago

wait for a while

ghost commented 8 years ago

@dashaus Provide an example for slices and <select html input control in the README.md :)

Also a suggestion for contribution, if you want you can join to iris-contrib organisation and I suggest to make a validation package also, more and more iris' users want this and I trust you that you will do your bests

xpbliss commented 8 years ago

The following is ok! maybe the iris's readForm should do a fix? @kataras

package main

import (
    "fmt"
    "github.com/monoculum/formam"
    "html/template"
    "net/http"
    "time"
)

type Visitor struct {
    Data       []string  `formam:"mydata"`
    Foundation time.Time `formam:"foundation"`
}

var templ = `<!DOCTYPE html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="POST" action="/post">
<select multiple="multiple" name="mydata"> 
<option value='one'>One</option>
<option value='two'>Two</option>
<option value='three'>Three</option>
<option value='four'>Four</option>
</select> <br />
  <input type="date" name="foundation" value="1946-05-07"/>
  <input type="submit"/>
</form>
</body>
</html>`

func Index(w http.ResponseWriter, r *http.Request) {
    t := template.New("Person template")
    t, err := t.Parse(templ)
    if err != nil {
        fmt.Println(err)
    }
    t.Execute(w, "")

}
func Admin(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.Method)
    visitor := &Visitor{}
    r.ParseForm()
    if err := formam.Decode(r.Form, visitor); err != nil {
        fmt.Println(err)
    }
    fmt.Println(visitor)

}

func main() {
    http.HandleFunc("/", Index)
    http.HandleFunc("/post", Admin)
    http.ListenAndServe(":8080", nil)
}
ghost commented 8 years ago

@xpbliss Fixed, upgrade iris with go get -u github.com/kataras/iris also don't forget to do this&Visitor{Data: make([]string, 0)}

xpbliss commented 8 years ago

@kataras , after `go get -u, it work well!

emilgpa commented 8 years ago

@kataras I'm doing a validation package: valider. I'm using it strongly in my project but not has README.md and not is finished it yet. So, I don't care that people want to collaborate with my package or to do one new again ;)