Common isomorphic functionality in one kit.

form.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // +build !clientonly
  6. package isokit
  7. import (
  8. "honnef.co/go/js/dom"
  9. )
  10. type Form interface {
  11. Validate() bool
  12. Fields() map[string]string
  13. Errors() map[string]string
  14. FormParams() *FormParams
  15. PrefillFields()
  16. SetFields(fields map[string]string)
  17. SetErrors(errors map[string]string)
  18. SetFormParams(formParams *FormParams)
  19. SetPrefillFields(prefillFields []string)
  20. }
  21. func FormValue(fp *FormParams, key string) string {
  22. var result string
  23. if OperatingEnvironment() == ServerEnvironment && fp.Request == nil {
  24. return ""
  25. }
  26. switch OperatingEnvironment() {
  27. case ServerEnvironment:
  28. if fp.UseFormFieldsForValidation == true {
  29. result = fp.FormFields[key]
  30. } else {
  31. result = fp.Request.FormValue(key)
  32. }
  33. case WebBrowserEnvironment:
  34. field := fp.FormElement.QuerySelector("[name=" + key + "]")
  35. switch field.(type) {
  36. case *dom.HTMLInputElement:
  37. result = field.(*dom.HTMLInputElement).Value
  38. case *dom.HTMLTextAreaElement:
  39. result = field.(*dom.HTMLTextAreaElement).Value
  40. case *dom.HTMLSelectElement:
  41. result = field.(*dom.HTMLSelectElement).Value
  42. }
  43. }
  44. return result
  45. }