constructors.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package forms
  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. Kind: "text",
  16. }
  17. }
  18. /* Generate new password field */
  19. func NewPasswordField(name string) *FormField {
  20. return &FormField{
  21. Name: name,
  22. Kind: "password",
  23. }
  24. }
  25. /* Generate a new checkbox field */
  26. func NewCheckboxField(name string, checked bool) *FormField {
  27. return &FormField{
  28. Name: name,
  29. Checked: checked,
  30. Kind: "checkbox",
  31. }
  32. }
  33. /* Generate a new CSRF field */
  34. func NewCsrfField(w http.ResponseWriter, r *http.Request, secure bool) *FormField {
  35. return &FormField{
  36. Name: csrfFieldName,
  37. Value: csrfToken(w, r, secure),
  38. Kind: "hidden",
  39. Sticky: true,
  40. Validators: ValidatorsList{
  41. ValidCSRF(r),
  42. },
  43. }
  44. }
  45. /* Generate a new hidden text field */
  46. func NewHiddenField(name string, value *string) *FormField {
  47. return &FormField{
  48. Name: name,
  49. Value: strFromPtr(value),
  50. Kind: "hidden",
  51. }
  52. }