|
@@ -0,0 +1,72 @@
|
|
1
|
+// The Isomorphic Go Project
|
|
2
|
+// Copyright (c) Wirecog, LLC. All rights reserved.
|
|
3
|
+// Use of this source code is governed by a BSD-style
|
|
4
|
+// license, which can be found in the LICENSE file.
|
|
5
|
+
|
|
6
|
+package isokit
|
|
7
|
+
|
|
8
|
+import "strings"
|
|
9
|
+
|
|
10
|
+type BasicForm struct {
|
|
11
|
+ formParams *FormParams
|
|
12
|
+
|
|
13
|
+ autofillFields []string
|
|
14
|
+ fields map[string]string
|
|
15
|
+ errors map[string]string
|
|
16
|
+}
|
|
17
|
+
|
|
18
|
+func (c *BasicForm) AutofillFields() []string {
|
|
19
|
+ return c.autofillFields
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+func (c *BasicForm) Fields() map[string]string {
|
|
23
|
+ return c.fields
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+func (c *BasicForm) Errors() map[string]string {
|
|
27
|
+ return c.errors
|
|
28
|
+}
|
|
29
|
+
|
|
30
|
+func (c *BasicForm) FormParams() *FormParams {
|
|
31
|
+ return c.formParams
|
|
32
|
+
|
|
33
|
+}
|
|
34
|
+
|
|
35
|
+func (c *BasicForm) SetAutofillFields(autofillFields []string) {
|
|
36
|
+ c.autofillFields = autofillFields
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+func (c *BasicForm) SetFields(fields map[string]string) {
|
|
40
|
+ c.fields = fields
|
|
41
|
+}
|
|
42
|
+
|
|
43
|
+func (c *BasicForm) SetErrors(errors map[string]string) {
|
|
44
|
+ c.errors = errors
|
|
45
|
+}
|
|
46
|
+
|
|
47
|
+func (c *BasicForm) SetFormParams(formParams *FormParams) {
|
|
48
|
+ c.formParams = formParams
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+func (c *BasicForm) SetError(key string, message string) {
|
|
52
|
+ c.errors[key] = message
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+func (c *BasicForm) ClearErrors() {
|
|
56
|
+ c.errors = make(map[string]string)
|
|
57
|
+}
|
|
58
|
+
|
|
59
|
+func (c *BasicForm) PopulateFields() {
|
|
60
|
+ for _, fieldName := range c.autofillFields {
|
|
61
|
+ c.fields[fieldName] = FormValue(c.FormParams(), fieldName)
|
|
62
|
+ }
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+func (c *BasicForm) DisplayErrors() {
|
|
66
|
+ if OperatingEnvironment() == WebBrowserEnvironment && c.formParams.FormElement != nil {
|
|
67
|
+ errorSpans := c.formParams.FormElement.QuerySelectorAll(".formError")
|
|
68
|
+ for _, v := range errorSpans {
|
|
69
|
+ v.SetInnerHTML(c.errors[strings.Replace(v.GetAttribute("id"), "Error", "", -1)])
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+}
|