Common isomorphic functionality in one kit.

form.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. AutofillFields() []string
  13. Fields() map[string]string
  14. Errors() map[string]string
  15. // SetError(key string, message string)
  16. }
  17. func FormValue(fp *FormParams, key string) string {
  18. var result string
  19. if OperatingEnvironment() == ServerEnvironment && fp.Request == nil {
  20. return ""
  21. }
  22. switch OperatingEnvironment() {
  23. case ServerEnvironment:
  24. if fp.UseFormFieldsForValidation == true {
  25. result = fp.FormFields[key]
  26. } else {
  27. result = fp.Request.FormValue(key)
  28. }
  29. case WebBrowserEnvironment:
  30. field := fp.FormElement.QuerySelector("[name=" + key + "]")
  31. switch field.(type) {
  32. case *dom.HTMLInputElement:
  33. result = field.(*dom.HTMLInputElement).Value
  34. case *dom.HTMLTextAreaElement:
  35. result = field.(*dom.HTMLTextAreaElement).Value
  36. case *dom.HTMLSelectElement:
  37. result = field.(*dom.HTMLSelectElement).Value
  38. }
  39. }
  40. return result
  41. }