forms.go 3.5 KB

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