2021-01-26 08:36:53 -07:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-17 01:58:10 -07:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 08:36:53 -07:00
|
|
|
)
|
|
|
|
|
2021-08-10 17:31:13 -07: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 17:31:13 -07: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 17:31:13 -07:00
|
|
|
if v, ok := ctx.Req.Form[key]; ok {
|
2021-01-26 08:36:53 -07:00
|
|
|
return v
|
|
|
|
}
|
2021-08-10 17:31:13 -07:00
|
|
|
return nil
|
2021-01-26 08:36:53 -07:00
|
|
|
}
|
|
|
|
|
2021-08-10 17:31:13 -07: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 17:31:13 -07: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 17:31:13 -07: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
|
|
|
|
}
|
|
|
|
|
2021-08-10 17:31:13 -07:00
|
|
|
// FormBool returns true if the value for the provided key in the form is "1" or "true"
|
|
|
|
func (ctx *Context) FormBool(key string) bool {
|
|
|
|
v, _ := strconv.ParseBool(ctx.Req.FormValue(key))
|
2021-01-26 08:36:53 -07:00
|
|
|
return v
|
|
|
|
}
|
2021-06-17 01:58:10 -07:00
|
|
|
|
2021-08-10 17:31:13 -07: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 01:58:10 -07:00
|
|
|
if len(value) == 0 {
|
|
|
|
return util.OptionalBoolNone
|
|
|
|
}
|
2021-08-10 17:31:13 -07:00
|
|
|
v, _ := strconv.ParseBool(ctx.Req.FormValue(key))
|
2021-06-17 01:58:10 -07:00
|
|
|
return util.OptionalBoolOf(v)
|
|
|
|
}
|