forms.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. Class string
  20. Type string
  21. Placeholder string
  22. Help string
  23. Required bool
  24. AutoFocus bool
  25. Sticky bool
  26. Validators ValidatorsList
  27. }
  28. /* AddError appends a new error message to the form field errors list */
  29. func (f *FormField) AddError(err error) *FormField {
  30. f.Error = append(f.Error, err)
  31. return f
  32. }
  33. /* Add AddValidator appends a new validator to the form field validators list */
  34. func (f *FormField) AddValidator(validator ValidatorFunc) *FormField {
  35. f.Validators = append(f.Validators, validator)
  36. return f
  37. }
  38. /* SetValidators configures validators list in form field */
  39. func (f *FormField) SetValidators(validators ValidatorsList) *FormField {
  40. f.Validators = validators
  41. return f
  42. }
  43. /* SetLabel configures form label */
  44. func (f *FormField) SetLabel(label string) *FormField {
  45. f.Label = label
  46. return f
  47. }
  48. /* SetClass configures class name in form field */
  49. func (f *FormField) SetClass(class string) *FormField {
  50. f.Class = class
  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. /* SetType specifies input field type */
  64. func (f *FormField) SetType(t string) *FormField {
  65. f.Type = 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. /* GetString returns FormField.Value as string */
  79. func (f *FormField) GetString() string {
  80. return f.Value
  81. }
  82. /* GetInt returns FormField.Value as int */
  83. func (f FormField) GetInt() (v int, err error) {
  84. v, err = strconv.Atoi(f.Value)
  85. if err != nil {
  86. err = EInvalidIntValue
  87. }
  88. return
  89. }
  90. /* Int converts FormField.Value to integer value and ignores errors */
  91. func (f *FormField) Int() int {
  92. if result, err := strconv.Atoi(f.Value); err == nil {
  93. return result
  94. }
  95. return 0
  96. }
  97. /* GetFloat returns FormField.Value as float */
  98. func (f *FormField) GetFloat() (float64, error) {
  99. return strconv.ParseFloat(f.Value, 64)
  100. }
  101. /* Float converts FormField.Value to float and ignores errors */
  102. func (f *FormField) Float() float64 {
  103. if result, err := strconv.ParseFloat(f.Value, 64); err == nil {
  104. return result
  105. }
  106. return 0.0
  107. }
  108. /* GetBool returns boolean value for checkbox fields */
  109. func (f *FormField) GetBool() (bool, error) {
  110. /* placeholder */
  111. return false, nil
  112. }
  113. /* GetChecked returns true if checkbox has been checked and its value is "on" */
  114. func (f *FormField) GetChecked() bool {
  115. return f.Value == "on"
  116. }
  117. /* formFieldTemplate is a template to render FormField element in HTML format */
  118. //go:embed templates/*
  119. var formTemplates embed.FS
  120. /* must checks for compile/start up errors */
  121. func must(r any, e error) any {
  122. if e != nil {
  123. panic(e)
  124. }
  125. return r
  126. }
  127. /* formTemplate is compiled template to render FormField element in HTML format */
  128. var formTemplate = template.Must(
  129. template.New("FormField").Parse(
  130. string(must(formTemplates.ReadFile("templates/field.html")).([]byte)),
  131. ),
  132. )
  133. /* HTML renders FormField element in html format */
  134. func (f *FormField) HTML() template.HTML {
  135. var buffer bytes.Buffer
  136. if err := formTemplate.Execute(&buffer, f); err == nil {
  137. return template.HTML(buffer.String())
  138. }
  139. return template.HTML("")
  140. }