Common isomorphic functionality in one kit.

basicform.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. autofillFields []string
  10. fields map[string]string
  11. errors map[string]string
  12. }
  13. func (c *BasicForm) AutofillFields() []string {
  14. return c.autofillFields
  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) SetAutofillFields(autofillFields []string) {
  26. c.autofillFields = autofillFields
  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.autofillFields {
  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. }