Common isomorphic functionality in one kit.

form.go 1.2KB

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