-
Notifications
You must be signed in to change notification settings - Fork 1
/
form.go
74 lines (61 loc) · 1.54 KB
/
form.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package forms
import (
"net/url"
)
type FormValues map[string]interface{}
type FormValidator func(formValues *FormValues) bool
type Form struct {
Fields map[string]*Field
FieldNames []string
Errors map[string]error
Values FormValues
validator FormValidator
}
func New() *Form {
return &Form{
Fields: make(map[string]*Field),
FieldNames: make([]string, 0),
}
}
// WithField adds the Field produced by the FieldBuilder to the Form under the given name.
func (f *Form) WithField(name string, fb *FieldBuilder) *Form {
field := fb.Build()
f.Fields[name] = field
f.FieldNames = append(f.FieldNames, name)
return f
}
// WithValidator adds the FormValidator to the form.
func (f *Form) WithValidator(validator FormValidator) *Form {
f.validator = validator
return f
}
// Valid validates each field followed by the form's validator if provided.
func (f *Form) Valid(postForm url.Values) bool {
valid := true
f.Errors = nil
f.Values = nil
formValues := make(FormValues)
formErrors := make(map[string]error)
// validate fields
for _, fname := range f.FieldNames {
fieldValue, fieldError := f.Fields[fname].Validate(postForm.Get(fname))
if fieldError != nil {
valid = false
formErrors[fname] = fieldError
} else {
formValues[fname] = fieldValue
}
}
// validate form
if valid && f.validator != nil {
valid = f.validator(&formValues)
}
// if its valid, make the values available
// otherwise make the errors available
if valid {
f.Values = formValues
} else {
f.Errors = formErrors
}
return valid
}