forms.go 4.2 KB

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