-
Notifications
You must be signed in to change notification settings - Fork 2
/
yes_no_form.go
46 lines (35 loc) · 901 Bytes
/
yes_no_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
package form
import (
"net/http"
"strings"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type YesNoForm struct {
YesNo YesNo
Error error
ErrorLabel string
Options YesNoOptions
FieldName string
}
func ReadYesNoForm(r *http.Request, errorLabel string) *YesNoForm {
form := NewYesNoForm(YesNoUnknown)
form.YesNo, form.Error = ParseYesNo(PostFormString(r, form.FieldName))
form.ErrorLabel = errorLabel
return form
}
func (f *YesNoForm) Validate() validation.List {
var errors validation.List
errors.Error(f.FieldName, f.ErrorLabel, f.Error,
validation.Selected())
return errors
}
func PostFormString(r *http.Request, name string) string {
return strings.TrimSpace(r.PostFormValue(name))
}
func NewYesNoForm(yesNo YesNo) *YesNoForm {
return &YesNoForm{
YesNo: yesNo,
Options: YesNoValues,
FieldName: FieldNames.YesNo,
}
}