constructors.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package form
  2. import "net/http"
  3. /* strFromPtr converts string pointer to a string */
  4. func strFromPtr(str *string) (s string) {
  5. if str != nil {
  6. s = *str
  7. }
  8. return
  9. }
  10. /* Generate new CharField field with type text */
  11. func NewCharField(Name string, Value *string) *FormField {
  12. return &FormField{
  13. Name: Name,
  14. Value: strFromPtr(Value),
  15. Class: "form-control",
  16. Type: "text",
  17. }
  18. }
  19. /* Generate a new hidden text field */
  20. func NewHiddenField(Name string, Value *string) *FormField {
  21. return &FormField{
  22. Name: Name,
  23. Value: strFromPtr(Value),
  24. Class: "form-control",
  25. Type: "hidden",
  26. }
  27. }
  28. /* Generate a new CSRF field */
  29. func NewCsrfField(w http.ResponseWriter, r *http.Request, secure bool) *FormField {
  30. return &FormField{
  31. Name: csrfFieldName,
  32. Value: csrfToken(w, r, secure),
  33. Class: "form-control",
  34. Type: "hidden",
  35. Sticky: true,
  36. Validators: ValidatorsList{
  37. ValidCSRF(r),
  38. },
  39. }
  40. }
  41. /* Generate new CharField field with type password */
  42. func NewPasswordField(Name string) *FormField {
  43. field := NewCharField(Name, nil).SetType("password")
  44. return field
  45. }