summaryrefslogtreecommitdiff
path: root/form.go
blob: c5dffa05bbcdfeee147048de4298c1d1e8229723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/schema"
)

var schemaDecoder *schema.Decoder

func init() {
	schemaDecoder = schema.NewDecoder()
}

type fieldError struct {
	Field string
	Issue string
}

func (fe fieldError) Error() string {
	return fmt.Sprintf("%s: %v", fe.Field, fe.Issue)
}

func (fe fieldError) FieldError() (field, err string) {
	return fe.Field, fe.Issue
}

func parseForm[T any](r *http.Request, data *T) {
	if err := r.ParseForm(); err != nil {
		log.Panic("Parsing form: ", err)
	}
	if err := schemaDecoder.Decode(data, r.PostForm); err != nil {
		log.Panic("Decoding form: ", err)
	}
}