forms.go 944 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package form
  2. import (
  3. "context"
  4. "strconv"
  5. )
  6. // ValidatorFunc defines a function for FormField data validation
  7. type ValidatorFunc func(FormField, context.Context) error
  8. // ValidatorsList defines a list of ValidatorFunc
  9. type ValidatorsList []ValidatorFunc
  10. // A general purpose form field struct
  11. type FormField struct {
  12. Name string
  13. Type string
  14. Label string
  15. Value string
  16. Error error
  17. Validators *ValidatorsList
  18. }
  19. // GetString returns FormField.Value as string
  20. func (f FormField) GetString() string {
  21. return f.Value
  22. }
  23. // GetInt returns FormField.Value as int
  24. func (f FormField) GetInt() (int, error) {
  25. return strconv.Atoi(f.Value)
  26. }
  27. // GetFloat returns FormField.Value as float
  28. func (f FormField) GetFloat() (float64, error) {
  29. return strconv.ParseFloat(f.Value, 64)
  30. }
  31. // GetBool returns boolean value for checkbox fields
  32. func (f FormField) GetBool() (bool, error) {
  33. // placeholder
  34. return false, nil
  35. }