-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformulate.go
54 lines (42 loc) · 1.96 KB
/
formulate.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package formulate
import (
"bytes"
"html/template"
"io"
"net/http"
"net/url"
)
// Formulate is an all-in-one method for handling form encoding and decoding, including validation errors.
// This expects the form to be POST-ed to the same endpoint as the form is displayed on. If you require a custom
// implementation of the form handling (including on separate endpoints), this can be done with the
// HTMLEncoder.Encode and HTTPDecoder.Decode methods.
// The Formulate method overrides any ValidationStore already set and uses a MemoryValidationStore instead.
func Formulate(r *http.Request, data interface{}, encoderBuilder HTMLEncoderBuilder, decoderBuilder HTTPDecoderBuilder) (encodedForm template.HTML, passedValidation bool, err error) {
validationStore := NewMemoryValidationStore()
if r.Method == http.MethodPost {
if err = r.ParseForm(); err != nil {
return "", passedValidation, err
}
decoder := decoderBuilder(r, r.Form)
decoder.SetValidationStore(validationStore)
err := decoder.Decode(data)
if err == nil {
passedValidation = true
} else if err != ErrFormFailedValidation {
return "", passedValidation, err
}
}
buf := new(bytes.Buffer)
encoder := encoderBuilder(r, buf)
encoder.SetValidationStore(validationStore)
if err := encoder.Encode(data); err != nil {
return "", passedValidation, err
}
return template.HTML(buf.Bytes()), passedValidation, nil
}
// HTMLEncoderBuilder is a function that builds a HTMLEncoder given an io.Writer as the output.
// When used with the Formulate method, this allows for custom building of the encoder (including ShowConditions etc).
type HTMLEncoderBuilder func(r *http.Request, w io.Writer) *HTMLEncoder
// HTTPDecoderBuilder is a function that builds a HTTPDecoder given the form as the input.
// When used with the Formulate method, this allows for custom building of the decoder (including ShowConditions, Validators etc).
type HTTPDecoderBuilder func(r *http.Request, values url.Values) *HTTPDecoder