Browse Source

Isomorphic form functionality.

Wirecog 7 years ago
parent
commit
f15759bef7
2 changed files with 129 additions and 0 deletions
  1. 72
    0
      basicform.go
  2. 57
    0
      form.go

+ 72
- 0
basicform.go View File

@@ -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
+}

+ 57
- 0
form.go View File

@@ -0,0 +1,57 @@
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 (
9
+	"net/http"
10
+
11
+	"honnef.co/go/js/dom"
12
+)
13
+
14
+type Form interface {
15
+	Validate() bool
16
+	AutofillFields() []string
17
+	Fields() map[string]string
18
+	Errors() map[string]string
19
+	//	SetError(key string, message string)
20
+}
21
+
22
+type FormParams struct {
23
+	ResponseWriter http.ResponseWriter
24
+	Request        *http.Request
25
+	FormElement    *dom.HTMLFormElement
26
+}
27
+
28
+func FormValue(fp *FormParams, key string) string {
29
+
30
+	var result string
31
+
32
+	if OperatingEnvironment() == ServerEnvironment && fp.Request == nil {
33
+		return ""
34
+	}
35
+
36
+	switch OperatingEnvironment() {
37
+
38
+	case ServerEnvironment:
39
+		result = fp.Request.FormValue(key)
40
+
41
+	case WebBrowserEnvironment:
42
+
43
+		field := fp.FormElement.QuerySelector("[name=" + key + "]")
44
+
45
+		switch field.(type) {
46
+		case *dom.HTMLInputElement:
47
+			result = field.(*dom.HTMLInputElement).Value
48
+		case *dom.HTMLTextAreaElement:
49
+			result = field.(*dom.HTMLTextAreaElement).Value
50
+		case *dom.HTMLSelectElement:
51
+			result = field.(*dom.HTMLSelectElement).Value
52
+
53
+		}
54
+	}
55
+
56
+	return result
57
+}