2021-01-26 15:36:53 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 15:36:53 +00:00
|
|
|
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-17 08:58:10 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 15:36:53 +00:00
|
|
|
)
|
|
|
|
|
2021-08-11 00:31:13 +00: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 15:36:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 00:31:13 +00: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 15:36:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-11 00:31:13 +00:00
|
|
|
if v, ok := ctx.Req.Form[key]; ok {
|
2021-01-26 15:36:53 +00:00
|
|
|
return v
|
|
|
|
}
|
2021-08-11 00:31:13 +00:00
|
|
|
return nil
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 00:31:13 +00: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 15:36:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 00:31:13 +00: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 15:36:53 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-08-11 00:31:13 +00: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 15:36:53 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:59:26 +00:00
|
|
|
// FormBool returns true if the value for the provided key in the form is "1", "true" or "on"
|
2021-08-11 00:31:13 +00:00
|
|
|
func (ctx *Context) FormBool(key string) bool {
|
2022-01-21 17:59:26 +00:00
|
|
|
s := ctx.Req.FormValue(key)
|
|
|
|
v, _ := strconv.ParseBool(s)
|
|
|
|
v = v || strings.EqualFold(s, "on")
|
2021-01-26 15:36:53 +00:00
|
|
|
return v
|
|
|
|
}
|
2021-06-17 08:58:10 +00:00
|
|
|
|
2021-08-11 00:31:13 +00: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 08:58:10 +00:00
|
|
|
if len(value) == 0 {
|
|
|
|
return util.OptionalBoolNone
|
|
|
|
}
|
2022-01-21 17:59:26 +00:00
|
|
|
s := ctx.Req.FormValue(key)
|
|
|
|
v, _ := strconv.ParseBool(s)
|
|
|
|
v = v || strings.EqualFold(s, "on")
|
2021-06-17 08:58:10 +00:00
|
|
|
return util.OptionalBoolOf(v)
|
|
|
|
}
|
2023-05-06 14:04:55 +00:00
|
|
|
|
|
|
|
func (ctx *Context) SetFormString(key, value string) {
|
|
|
|
_ = ctx.Req.FormValue(key) // force parse form
|
|
|
|
ctx.Req.Form.Set(key, value)
|
|
|
|
}
|