You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

form.go 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "strconv"
  7. "strings"
  8. "code.gitea.io/gitea/modules/util"
  9. )
  10. // FormString returns the first value matching the provided key in the form as a string
  11. func (ctx *Context) FormString(key string) string {
  12. return ctx.Req.FormValue(key)
  13. }
  14. // FormStrings returns a string slice for the provided key from the form
  15. func (ctx *Context) FormStrings(key string) []string {
  16. if ctx.Req.Form == nil {
  17. if err := ctx.Req.ParseMultipartForm(32 << 20); err != nil {
  18. return nil
  19. }
  20. }
  21. if v, ok := ctx.Req.Form[key]; ok {
  22. return v
  23. }
  24. return nil
  25. }
  26. // FormTrim returns the first value for the provided key in the form as a space trimmed string
  27. func (ctx *Context) FormTrim(key string) string {
  28. return strings.TrimSpace(ctx.Req.FormValue(key))
  29. }
  30. // FormInt returns the first value for the provided key in the form as an int
  31. func (ctx *Context) FormInt(key string) int {
  32. v, _ := strconv.Atoi(ctx.Req.FormValue(key))
  33. return v
  34. }
  35. // FormInt64 returns the first value for the provided key in the form as an int64
  36. func (ctx *Context) FormInt64(key string) int64 {
  37. v, _ := strconv.ParseInt(ctx.Req.FormValue(key), 10, 64)
  38. return v
  39. }
  40. // FormBool returns true if the value for the provided key in the form is "1" or "true"
  41. func (ctx *Context) FormBool(key string) bool {
  42. v, _ := strconv.ParseBool(ctx.Req.FormValue(key))
  43. return v
  44. }
  45. // FormOptionalBool returns an OptionalBoolTrue or OptionalBoolFalse if the value
  46. // for the provided key exists in the form else it returns OptionalBoolNone
  47. func (ctx *Context) FormOptionalBool(key string) util.OptionalBool {
  48. value := ctx.Req.FormValue(key)
  49. if len(value) == 0 {
  50. return util.OptionalBoolNone
  51. }
  52. v, _ := strconv.ParseBool(ctx.Req.FormValue(key))
  53. return util.OptionalBoolOf(v)
  54. }