2021-01-26 08:36:53 -07:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 08:36:53 -07:00
|
|
|
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-17 02:58:10 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 08:36:53 -07:00
|
|
|
)
|
|
|
|
|
2021-08-10 18:31:13 -06:00
|
|
|
// FormString returns the first value matching the provided key in the form as a string
|
|
|
|
func (ctx *Context) FormString(key string) string {
|
|
|
|
return ctx.Req.FormValue(key)
|
2021-01-26 08:36:53 -07:00
|
|
|
}
|
|
|
|
|
2021-08-10 18:31:13 -06:00
|
|
|
// FormStrings returns a string slice for the provided key from the form
|
|
|
|
func (ctx *Context) FormStrings(key string) []string {
|
|
|
|
if ctx.Req.Form == nil {
|
|
|
|
if err := ctx.Req.ParseMultipartForm(32 << 20); err != nil {
|
|
|
|
return nil
|
2021-01-26 08:36:53 -07:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 18:31:13 -06:00
|
|
|
if v, ok := ctx.Req.Form[key]; ok {
|
2021-01-26 08:36:53 -07:00
|
|
|
return v
|
|
|
|
}
|
2021-08-10 18:31:13 -06:00
|
|
|
return nil
|
2021-01-26 08:36:53 -07:00
|
|
|
}
|
|
|
|
|
2021-08-10 18:31:13 -06:00
|
|
|
// FormTrim returns the first value for the provided key in the form as a space trimmed string
|
|
|
|
func (ctx *Context) FormTrim(key string) string {
|
|
|
|
return strings.TrimSpace(ctx.Req.FormValue(key))
|
2021-01-26 08:36:53 -07:00
|
|
|
}
|
|
|
|
|
2021-08-10 18:31:13 -06:00
|
|
|
// FormInt returns the first value for the provided key in the form as an int
|
|
|
|
func (ctx *Context) FormInt(key string) int {
|
|
|
|
v, _ := strconv.Atoi(ctx.Req.FormValue(key))
|
2021-01-26 08:36:53 -07:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-08-10 18:31:13 -06:00
|
|
|
// FormInt64 returns the first value for the provided key in the form as an int64
|
|
|
|
func (ctx *Context) FormInt64(key string) int64 {
|
|
|
|
v, _ := strconv.ParseInt(ctx.Req.FormValue(key), 10, 64)
|
2021-01-26 08:36:53 -07:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-01-21 10:59:26 -07:00
|
|
|
// FormBool returns true if the value for the provided key in the form is "1", "true" or "on"
|
2021-08-10 18:31:13 -06:00
|
|
|
func (ctx *Context) FormBool(key string) bool {
|
2022-01-21 10:59:26 -07:00
|
|
|
s := ctx.Req.FormValue(key)
|
|
|
|
v, _ := strconv.ParseBool(s)
|
|
|
|
v = v || strings.EqualFold(s, "on")
|
2021-01-26 08:36:53 -07:00
|
|
|
return v
|
|
|
|
}
|
2021-06-17 02:58:10 -06:00
|
|
|
|
2021-08-10 18:31:13 -06:00
|
|
|
// FormOptionalBool returns an OptionalBoolTrue or OptionalBoolFalse if the value
|
|
|
|
// for the provided key exists in the form else it returns OptionalBoolNone
|
|
|
|
func (ctx *Context) FormOptionalBool(key string) util.OptionalBool {
|
|
|
|
value := ctx.Req.FormValue(key)
|
2021-06-17 02:58:10 -06:00
|
|
|
if len(value) == 0 {
|
|
|
|
return util.OptionalBoolNone
|
|
|
|
}
|
2022-01-21 10:59:26 -07:00
|
|
|
s := ctx.Req.FormValue(key)
|
|
|
|
v, _ := strconv.ParseBool(s)
|
|
|
|
v = v || strings.EqualFold(s, "on")
|
2021-06-17 02:58:10 -06:00
|
|
|
return util.OptionalBoolOf(v)
|
|
|
|
}
|