forms.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 ValidatorsList) *FormField {
  41. f.Validators = validators
  42. return f
  43. }
  44. /* SetLabel configures form label */
  45. func (f *FormField) SetLabel(label string) *FormField {
  46. f.Label = label
  47. return f
  48. }
  49. /* SetRequired marks FormField as mandatory */
  50. func (f *FormField) SetRequired() *FormField {
  51. f.Required = true
  52. return f
  53. }
  54. /* SetAutoFocus gives focus to current FormField on page load */
  55. func (f *FormField) SetAutoFocus() *FormField {
  56. f.AutoFocus = true
  57. return f
  58. }
  59. /* SetKind specifies input field type */
  60. func (f *FormField) SetKind(t string) *FormField {
  61. f.Kind = t
  62. return f
  63. }
  64. /* SetPlaceholder specified a placeholder property in Formfield */
  65. func (f *FormField) SetPlaceholder(placeholder string) *FormField {
  66. f.Placeholder = placeholder
  67. return f
  68. }
  69. /* SetHelp specified a help message for current Formfield */
  70. func (f *FormField) SetHelp(help string) *FormField {
  71. f.Help = help
  72. return f
  73. }
  74. /* SetAutoComplete sets the autocomplete flag for a field */
  75. func (f *FormField) SetAutoComplete(on bool) *FormField {
  76. f.AutoComplete = "off"
  77. if on {
  78. f.AutoComplete = "on"
  79. }
  80. return f
  81. }
  82. /* GetString returns FormField.Value as string */
  83. func (f *FormField) GetString() string {
  84. return f.Value
  85. }
  86. /* GetInt returns FormField.Value as int */
  87. func (f FormField) GetInt() (v int, err error) {
  88. v, err = strconv.Atoi(f.Value)
  89. if err != nil {
  90. err = EInvalidIntValue
  91. }
  92. return
  93. }
  94. /* Int converts FormField.Value to integer value and ignores errors */
  95. func (f *FormField) Int() int {
  96. if result, err := strconv.Atoi(f.Value); err == nil {
  97. return result
  98. }
  99. return 0
  100. }
  101. /* GetFloat returns FormField.Value as float */
  102. func (f *FormField) GetFloat() (float64, error) {
  103. return strconv.ParseFloat(f.Value, 64)
  104. }
  105. /* Float converts FormField.Value to float and ignores errors */
  106. func (f *FormField) Float() float64 {
  107. if result, err := strconv.ParseFloat(f.Value, 64); err == nil {
  108. return result
  109. }
  110. return 0.0
  111. }
  112. /* GetBool returns boolean value for checkbox fields */
  113. func (f *FormField) GetBool() (bool, error) {
  114. /* placeholder */
  115. return false, nil
  116. }
  117. /* GetChecked returns true if checkbox has been checked and its value is "on" */
  118. func (f *FormField) GetChecked() bool {
  119. return f.Value == "on"
  120. }
  121. /* formFieldTemplate is a template to render FormField element in HTML format */
  122. //go:embed fields.html
  123. var fieldsTemplateStr string
  124. /* must checks for compile/start up errors */
  125. func must(r any, e error) any {
  126. if e != nil {
  127. panic(e)
  128. }
  129. return r
  130. }
  131. /* fieldsTemplate is compiled template to render FormField element in HTML format */
  132. var fieldsTemplate = template.Must(
  133. template.New("FormField").Parse(fieldsTemplateStr),
  134. )
  135. /* HTML renders FormField element in html format */
  136. func (f *FormField) HTML() template.HTML {
  137. var buffer bytes.Buffer
  138. if err := fieldsTemplate.ExecuteTemplate(&buffer, f.Kind, f); err == nil {
  139. return template.HTML(buffer.String())
  140. }
  141. return template.HTML("")
  142. }