forms.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package forms
  2. import (
  3. "bytes"
  4. "context"
  5. _ "embed"
  6. "html/template"
  7. "strconv"
  8. )
  9. /* ValidatorFunc defines a function for FormField data validation */
  10. type ValidatorFunc func(*FormField, context.Context) error
  11. /* ValidatorsList defines a list of ValidatorFunc */
  12. type ValidatorsList []ValidatorFunc
  13. /* A general purpose form field struct */
  14. type FormField struct {
  15. Name string
  16. Error []error
  17. Value string
  18. Label string
  19. Kind string
  20. Placeholder string
  21. Help string
  22. Required bool
  23. AutoFocus bool
  24. Sticky bool
  25. Checked bool
  26. AutoComplete string
  27. Validators ValidatorsList
  28. }
  29. /* AddError appends a new error message to the form field errors list */
  30. func (f *FormField) AddError(err error) *FormField {
  31. f.Error = append(f.Error, err)
  32. return f
  33. }
  34. /* Add AddValidator appends a new validator to the form field validators list */
  35. func (f *FormField) AddValidator(validator ValidatorFunc) *FormField {
  36. f.Validators = append(f.Validators, validator)
  37. return f
  38. }
  39. /* SetValidators configures validators list in form field */
  40. func (f *FormField) SetValidators(validators ...ValidatorFunc) *FormField {
  41. for _, validator := range validators {
  42. f.AddValidator(validator)
  43. }
  44. return f
  45. }
  46. /* SetLabel configures form label */
  47. func (f *FormField) SetLabel(label string) *FormField {
  48. f.Label = label
  49. return f
  50. }
  51. /* SetRequired marks FormField as mandatory */
  52. func (f *FormField) SetRequired() *FormField {
  53. f.Required = true
  54. return f
  55. }
  56. /* SetAutoFocus gives focus to current FormField on page load */
  57. func (f *FormField) SetAutoFocus() *FormField {
  58. f.AutoFocus = true
  59. return f
  60. }
  61. /* SetKind specifies input field type */
  62. func (f *FormField) SetKind(t string) *FormField {
  63. f.Kind = t
  64. return f
  65. }
  66. /* SetPlaceholder specified a placeholder property in Formfield */
  67. func (f *FormField) SetPlaceholder(placeholder string) *FormField {
  68. f.Placeholder = placeholder
  69. return f
  70. }
  71. /* SetHelp specified a help message for current Formfield */
  72. func (f *FormField) SetHelp(help string) *FormField {
  73. f.Help = help
  74. return f
  75. }
  76. /* SetAutoComplete sets the autocomplete flag for a field */
  77. func (f *FormField) SetAutoComplete(on bool) *FormField {
  78. f.AutoComplete = "off"
  79. if on {
  80. f.AutoComplete = "on"
  81. }
  82. return f
  83. }
  84. /* GetString returns FormField.Value as string */
  85. func (f *FormField) GetString() string {
  86. return f.Value
  87. }
  88. /* GetInt returns FormField.Value as int */
  89. func (f FormField) GetInt() (v int, err error) {
  90. v, err = strconv.Atoi(f.Value)
  91. if err != nil {
  92. err = EInvalidIntValue
  93. }
  94. return
  95. }
  96. /* Int converts FormField.Value to integer value and ignores errors */
  97. func (f *FormField) Int() int {
  98. if result, err := strconv.Atoi(f.Value); err == nil {
  99. return result
  100. }
  101. return 0
  102. }
  103. /* GetFloat returns FormField.Value as float */
  104. func (f *FormField) GetFloat() (float64, error) {
  105. return strconv.ParseFloat(f.Value, 64)
  106. }
  107. /* Float converts FormField.Value to float and ignores errors */
  108. func (f *FormField) Float() float64 {
  109. if result, err := strconv.ParseFloat(f.Value, 64); err == nil {
  110. return result
  111. }
  112. return 0.0
  113. }
  114. /* GetBool returns true if checkbox is selected, always returns nil error */
  115. func (f *FormField) GetBool() (bool, error) {
  116. /* if box is checked, it sends a non-empty value */
  117. return len(f.Value) != 0, nil
  118. }
  119. /* Bool returns true if checkbox is selected, ignores errors */
  120. func (f *FormField) Bool() bool {
  121. return len(f.Value) != 0
  122. }
  123. /* fieldsTemplateStr is a template to render FormField element in HTML format */
  124. //go:embed fields.html
  125. var fieldsTemplateStr string
  126. /* must checks for compile/start up errors */
  127. func must(r any, e error) any {
  128. if e != nil {
  129. panic(e)
  130. }
  131. return r
  132. }
  133. /* fieldsTemplate is compiled template to render FormField element in HTML format */
  134. var fieldsTemplate = template.Must(
  135. template.New("FormField").Parse(fieldsTemplateStr),
  136. )
  137. /* HTML renders FormField element in html format */
  138. func (f *FormField) HTML() template.HTML {
  139. var buffer bytes.Buffer
  140. if err := fieldsTemplate.ExecuteTemplate(&buffer, f.Kind, f); err == nil {
  141. return template.HTML(buffer.String())
  142. }
  143. return template.HTML("")
  144. }