forms.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package form
  2. import (
  3. "bytes"
  4. "context"
  5. "html/template"
  6. "strconv"
  7. )
  8. /* ValidatorFunc defines a function for FormField data validation */
  9. type ValidatorFunc func(FormField, context.Context) error
  10. /* ValidatorsList defines a list of ValidatorFunc */
  11. type ValidatorsList []ValidatorFunc
  12. /* A general purpose form field struct */
  13. type FormField struct {
  14. Name string
  15. Error []error
  16. Value string
  17. Label string
  18. Class string
  19. Type string
  20. Placeholder string
  21. Help string
  22. Required bool
  23. AutoFocus bool
  24. Validators *ValidatorsList
  25. }
  26. /* SetValidators configures validators list in form field */
  27. func (f FormField) SetValidators(validators *ValidatorsList) FormField {
  28. f.Validators = validators
  29. return f
  30. }
  31. /* SetLabel configures form label */
  32. func (f FormField) SetLabel(label string) FormField {
  33. f.Label = label
  34. return f
  35. }
  36. /* SetClass configures class name in form field */
  37. func (f FormField) SetClass(class string) FormField {
  38. f.Class = class
  39. return f
  40. }
  41. /* SetRequired marks FormField as mandatory */
  42. func (f FormField) SetRequired() FormField {
  43. f.Required = true
  44. return f
  45. }
  46. /* SetAutoFocus gives focus to current FormField on page load */
  47. func (f FormField) SetAutoFocus() FormField {
  48. f.AutoFocus = true
  49. return f
  50. }
  51. /* SetType specifies input field type */
  52. func (f FormField) SetType(t string) FormField {
  53. f.Type = t
  54. return f
  55. }
  56. /* SetPlaceholder specified a placeholder property in Formfield */
  57. func (f FormField) SetPlaceholder(placeholder string) FormField {
  58. f.Placeholder = placeholder
  59. return f
  60. }
  61. /* SetHelp specified a help message for current Formfield */
  62. func (f FormField) SetHelp(help string) FormField {
  63. f.Help = help
  64. return f
  65. }
  66. /* GetString returns FormField.Value as string */
  67. func (f FormField) GetString() string {
  68. return f.Value
  69. }
  70. /* GetInt returns FormField.Value as int */
  71. func (f FormField) GetInt() (v int, err error) {
  72. v, err = strconv.Atoi(f.Value)
  73. if err != nil {
  74. err = EInvalidIntValue
  75. }
  76. return
  77. }
  78. /* Int converts FormField.Value to integer value and ignores errors */
  79. func (f FormField) Int() int {
  80. if result, err := strconv.Atoi(f.Value); err == nil {
  81. return result
  82. }
  83. return 0
  84. }
  85. /* GetFloat returns FormField.Value as float */
  86. func (f FormField) GetFloat() (float64, error) {
  87. return strconv.ParseFloat(f.Value, 64)
  88. }
  89. /* Float converts FormField.Value to float and ignores errors */
  90. func (f FormField) Float() float64 {
  91. if result, err := strconv.ParseFloat(f.Value, 64); err == nil {
  92. return result
  93. }
  94. return 0.0
  95. }
  96. /* GetBool returns boolean value for checkbox fields */
  97. func (f FormField) GetBool() (bool, error) {
  98. /* placeholder */
  99. return false, nil
  100. }
  101. /* GetChecked returns true if checkbox has been checked and its value is "on" */
  102. func (f FormField) GetChecked() bool {
  103. return f.Value == "on"
  104. }
  105. /* formFieldTemplate is a template to render FormField element in HTML format */
  106. const formFieldTemplate = `
  107. {{ if .Label }}<label class="form-label" for="{{ .Name }}">{{ .Label }}</label>{{ end }}
  108. <input type="{{ .Type }}" id="{{ .Name }}" name="{{ .Name }}"
  109. {{- if .Class }} class="{{ .Class }}"{{ end }}
  110. {{- if .Value }} value="{{ .Value }}"{{ end }}
  111. {{- if .Placeholder}} placeholder="{{ .Placeholder }}"{{ end }}
  112. {{- if .Help }} aria-describedby="{{ .Name }}Help"{{ end }}
  113. {{- if .Required }} required{{ end }}
  114. {{- if .AutoFocus }} autofocus{{ end }}>
  115. {{ if .Help }}<div id="{{ .Name }}Help" class="form-text">{{ .Help }}</div>{{ end }}
  116. {{ if .Error }}{{ range $e := .Error }}<div class="text-danger">{{ $e }}</div>{{ end }}{{ end }}
  117. `
  118. /* formTemplate is compiled template to render FormField element in HTML format */
  119. var formTemplate = template.Must(template.New("FormField").Parse(formFieldTemplate))
  120. /* HTML renders FormField element in html format */
  121. func (f FormField) HTML() template.HTML {
  122. var buffer bytes.Buffer
  123. if err := formTemplate.Execute(&buffer, f); err == nil {
  124. return template.HTML(buffer.String())
  125. }
  126. return template.HTML("")
  127. }