Common isomorphic functionality in one kit.

basicform.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "strings"
  7. type BasicForm struct {
  8. formParams *FormParams
  9. prefillFields []string
  10. fields map[string]string
  11. errors map[string]string
  12. }
  13. func (c *BasicForm) PrefillFields() []string {
  14. return c.prefillFields
  15. }
  16. func (c *BasicForm) Fields() map[string]string {
  17. return c.fields
  18. }
  19. func (c *BasicForm) Errors() map[string]string {
  20. return c.errors
  21. }
  22. func (c *BasicForm) FormParams() *FormParams {
  23. return c.formParams
  24. }
  25. func (c *BasicForm) SetPrefillFields(prefillFields []string) {
  26. c.prefillFields = prefillFields
  27. }
  28. func (c *BasicForm) SetFields(fields map[string]string) {
  29. c.fields = fields
  30. }
  31. func (c *BasicForm) SetErrors(errors map[string]string) {
  32. c.errors = errors
  33. }
  34. func (c *BasicForm) SetFormParams(formParams *FormParams) {
  35. c.formParams = formParams
  36. }
  37. func (c *BasicForm) SetError(key string, message string) {
  38. c.errors[key] = message
  39. }
  40. func (c *BasicForm) ClearErrors() {
  41. c.errors = make(map[string]string)
  42. }
  43. func (c *BasicForm) PopulateFields() {
  44. for _, fieldName := range c.prefillFields {
  45. c.fields[fieldName] = FormValue(c.FormParams(), fieldName)
  46. }
  47. }
  48. func (c *BasicForm) DisplayErrors() {
  49. if OperatingEnvironment() == WebBrowserEnvironment && c.formParams.FormElement != nil {
  50. errorSpans := c.formParams.FormElement.QuerySelectorAll(".formError")
  51. for _, v := range errorSpans {
  52. v.SetInnerHTML(c.errors[strings.Replace(v.GetAttribute("id"), "Error", "", -1)])
  53. }
  54. }
  55. }
  56. func (c *BasicForm) RegenerateErrors() {
  57. c.errors = make(map[string]string)
  58. if OperatingEnvironment() == WebBrowserEnvironment && c.formParams.FormElement != nil {
  59. errorSpans := c.formParams.FormElement.QuerySelectorAll(".formError")
  60. for _, v := range errorSpans {
  61. v.SetInnerHTML(c.errors[strings.Replace(v.GetAttribute("id"), "Error", "", -1)])
  62. }
  63. }
  64. }