webform

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 6f2c821548283df50eeea7f59d8c9fb0aec1fa49
parent 6a2a2abbad8350b21dd05bb976fd6eed4b1a2c99
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Mon, 22 Jul 2024 18:14:00 +0900

deel with "その他"

Diffstat:
Mmain.go | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
1 file changed, 57 insertions(+), 28 deletions(-)

diff --git a/main.go b/main.go @@ -56,7 +56,6 @@ type Question struct { OtherAns string } - func (*Question) isPart() bool { return false } func (q *Question) HTML() string { switch q.T { @@ -66,6 +65,9 @@ func (q *Question) HTML() string { id := q.Qid + strconv.Itoa(n) html += fmt.Sprintf("<input type=\"checkbox\" name=\"%s\", id=\"%s\" value=\"%d\">\n"+ "<label for=\"%s\">%s</label>\n", q.Qid, id, n, id, a) + if a == "その他" { + html += fmt.Sprintf("<input type=\"text\" name=\"%s_other\", id=\"%s_other\">\n", q.Qid, id) + } } html += "</fieldset>\n" return html @@ -75,6 +77,9 @@ func (q *Question) HTML() string { id := q.Qid + strconv.Itoa(n) html += fmt.Sprintf("<input type=\"radio\" name=\"%s\", id=\"%s\" value=\"%d\">\n"+ "<label for=\"%s\">%s</label>\n", q.Qid, id, n, id, a) + if a == "その他" { + html += fmt.Sprintf("<input type=\"text\" name=\"%s_other\", id=\"%s_other\">\n", q.Qid, id) + } } html += "</fieldset>\n" return html @@ -96,38 +101,53 @@ func (q *Question) makeAns(ans map[string][]string) ([]Ans, error) { case "check": var l []string for _, i := range a { + if i == "" { // empty answer for "その他" + continue + } n, err := strconv.Atoi(i) if err != nil { - return nil, fmt.Errorf("invalid answer %q for %s.", - i, q.Qid) + return nil, fmt.Errorf("invalid answer %q for %s.", i, q.Qid) } if n < 0 || len(q.CheckList) <= n { - return nil, fmt.Errorf("invalid answer %q for %s.", - n, q.Qid) + return nil, fmt.Errorf("invalid answer %q for %s.", n, q.Qid) + } + if q.CheckList[n] == "その他" { + var other string + if l, ok := ans[q.Qid+"_other"]; ok { + if len(l) > 0 { + other = l[0] + } + } + l = append(l, "その他:"+other) + } else { + l = append(l, q.CheckList[n]) } - l = append(l, q.CheckList[n]) } return []Ans{CheckAns{Qid: q.Qid, Type: q.T, Ans: l}}, nil case "radio": if len(a) != 1 { - return nil, fmt.Errorf("length of radio answer for %s is %d.\n", - q.Qid, len(a)) + return nil, fmt.Errorf("invalid length %d for %s.\n", len(a), q.Qid) } - n, err := strconv.Atoi(a[0]) + i, err := strconv.Atoi(a[0]) if err != nil { - return nil, fmt.Errorf("invalid answer %q for %s.", - a[0], q.Qid) + return nil, fmt.Errorf("invalid answer %s for %s.", a[0], q.Qid) } - if n < 0 || len(q.RadioList) <= n { - return nil, fmt.Errorf("invalid answer %q for %s.", - n, q.Qid) + if i < 0 || len(q.RadioList) <= i { + return nil, fmt.Errorf("invalid answer %s for %s.", a[0], q.Qid) } - // TODO: other - return []Ans{RadioAns{Qid: q.Qid, Type: q.T, Ans: a[0]}}, nil + if q.RadioList[i] == "その他" { + var other string + if l, ok := ans[q.Qid+"_other"]; ok { + if len(l) > 0 { + other = l[0] + } + } + return []Ans{RadioAns{Qid: q.Qid, Type: q.T, Ans: "その他:" + other}}, nil + } + return []Ans{RadioAns{Qid: q.Qid, Type: q.T, Ans: q.RadioList[i]}}, nil case "text": if len(a) != 1 { - return nil, fmt.Errorf("length of text answer for %s is %d.\n", - q.Qid, len(a)) + return nil, fmt.Errorf("invalid length %d for %s.\n", len(a), q.Qid) } return []Ans{TextAns{Qid: q.Qid, Type: q.T, Ans: a[0]}}, nil default: @@ -139,25 +159,25 @@ type Ans interface { } type TextAns struct { - Qid string + Qid string Type string - Ans string + Ans string } -type RadioAns struct{ - Qid string +type RadioAns struct { + Qid string Type string - Ans string + Ans string } -type CheckAns struct{ - Qid string +type CheckAns struct { + Qid string Type string - Ans []string + Ans []string } func formatAns(q Part, ans map[string][]string) ([]byte, error) { - return nil,nil + return nil, nil } var Q Part = Part{ @@ -190,6 +210,14 @@ var Q Part = Part{ "男性", "女性", }, }, + &Question{ + Q: "住所", + Qid: "address", + T: "radio", + RadioList: []string{ + "和歌山市", "海南市", "その他", + }, + }, }, }, &Part{ @@ -240,6 +268,7 @@ func handler(w http.ResponseWriter, r *http.Request) { return } fmt.Fprint(w, exitPage) + log.Printf("ans: %q\n", r.PostForm) a, err := Q.makeAns(r.PostForm) if err != nil { log.Printf("makeAns: %v\n", err) @@ -266,7 +295,7 @@ func writer(intc <-chan os.Signal, listenErrChan <-chan struct{}, L: for { select { - case q := <- c: + case q := <-c: if err := json.Compact(&buf, q); err != nil { log.Printf("error: compact: %v\n", err) continue