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