github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_request_param_form.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package ghttp 8 9 import ( 10 "github.com/gogf/gf/container/gvar" 11 "github.com/gogf/gf/util/gconv" 12 ) 13 14 // SetForm sets custom form value with key-value pair. 15 func (r *Request) SetForm(key string, value interface{}) { 16 r.parseForm() 17 if r.formMap == nil { 18 r.formMap = make(map[string]interface{}) 19 } 20 r.formMap[key] = value 21 } 22 23 // GetForm retrieves and returns parameter <key> from form. 24 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 25 func (r *Request) GetForm(key string, def ...interface{}) interface{} { 26 r.parseForm() 27 if len(r.formMap) > 0 { 28 if v, ok := r.formMap[key]; ok { 29 return v 30 } 31 } 32 if len(def) > 0 { 33 return def[0] 34 } 35 return nil 36 } 37 38 // GetFormVar retrieves and returns parameter <key> from form as Var. 39 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 40 func (r *Request) GetFormVar(key string, def ...interface{}) *gvar.Var { 41 return gvar.New(r.GetForm(key, def...)) 42 } 43 44 // GetFormString retrieves and returns parameter <key> from form as string. 45 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 46 func (r *Request) GetFormString(key string, def ...interface{}) string { 47 return r.GetFormVar(key, def...).String() 48 } 49 50 // GetFormBool retrieves and returns parameter <key> from form as bool. 51 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 52 func (r *Request) GetFormBool(key string, def ...interface{}) bool { 53 return r.GetFormVar(key, def...).Bool() 54 } 55 56 // GetFormInt retrieves and returns parameter <key> from form as int. 57 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 58 func (r *Request) GetFormInt(key string, def ...interface{}) int { 59 return r.GetFormVar(key, def...).Int() 60 } 61 62 // GetFormInt32 retrieves and returns parameter <key> from form as int32. 63 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 64 func (r *Request) GetFormInt32(key string, def ...interface{}) int32 { 65 return r.GetFormVar(key, def...).Int32() 66 } 67 68 // GetFormInt64 retrieves and returns parameter <key> from form as int64. 69 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 70 func (r *Request) GetFormInt64(key string, def ...interface{}) int64 { 71 return r.GetFormVar(key, def...).Int64() 72 } 73 74 // GetFormInts retrieves and returns parameter <key> from form as []int. 75 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 76 func (r *Request) GetFormInts(key string, def ...interface{}) []int { 77 return r.GetFormVar(key, def...).Ints() 78 } 79 80 // GetFormUint retrieves and returns parameter <key> from form as uint. 81 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 82 func (r *Request) GetFormUint(key string, def ...interface{}) uint { 83 return r.GetFormVar(key, def...).Uint() 84 } 85 86 // GetFormUint32 retrieves and returns parameter <key> from form as uint32. 87 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 88 func (r *Request) GetFormUint32(key string, def ...interface{}) uint32 { 89 return r.GetFormVar(key, def...).Uint32() 90 } 91 92 // GetFormUint64 retrieves and returns parameter <key> from form as uint64. 93 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 94 func (r *Request) GetFormUint64(key string, def ...interface{}) uint64 { 95 return r.GetFormVar(key, def...).Uint64() 96 } 97 98 // GetFormFloat32 retrieves and returns parameter <key> from form as float32. 99 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 100 func (r *Request) GetFormFloat32(key string, def ...interface{}) float32 { 101 return r.GetFormVar(key, def...).Float32() 102 } 103 104 // GetFormFloat64 retrieves and returns parameter <key> from form as float64. 105 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 106 func (r *Request) GetFormFloat64(key string, def ...interface{}) float64 { 107 return r.GetFormVar(key, def...).Float64() 108 } 109 110 // GetFormFloats retrieves and returns parameter <key> from form as []float64. 111 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 112 func (r *Request) GetFormFloats(key string, def ...interface{}) []float64 { 113 return r.GetFormVar(key, def...).Floats() 114 } 115 116 // GetFormArray retrieves and returns parameter <key> from form as []string. 117 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 118 func (r *Request) GetFormArray(key string, def ...interface{}) []string { 119 return r.GetFormVar(key, def...).Strings() 120 } 121 122 // GetFormStrings retrieves and returns parameter <key> from form as []string. 123 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 124 func (r *Request) GetFormStrings(key string, def ...interface{}) []string { 125 return r.GetFormVar(key, def...).Strings() 126 } 127 128 // GetFormInterfaces retrieves and returns parameter <key> from form as []interface{}. 129 // It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil. 130 func (r *Request) GetFormInterfaces(key string, def ...interface{}) []interface{} { 131 return r.GetFormVar(key, def...).Interfaces() 132 } 133 134 // GetFormMap retrieves and returns all form parameters passed from client as map. 135 // The parameter <kvMap> specifies the keys retrieving from client parameters, 136 // the associated values are the default values if the client does not pass. 137 func (r *Request) GetFormMap(kvMap ...map[string]interface{}) map[string]interface{} { 138 r.parseForm() 139 if len(kvMap) > 0 && kvMap[0] != nil { 140 if len(r.formMap) == 0 { 141 return kvMap[0] 142 } 143 m := make(map[string]interface{}, len(kvMap[0])) 144 for k, defValue := range kvMap[0] { 145 if postValue, ok := r.formMap[k]; ok { 146 m[k] = postValue 147 } else { 148 m[k] = defValue 149 } 150 } 151 return m 152 } else { 153 return r.formMap 154 } 155 } 156 157 // GetFormMapStrStr retrieves and returns all form parameters passed from client as map[string]string. 158 // The parameter <kvMap> specifies the keys retrieving from client parameters, the associated values 159 // are the default values if the client does not pass. 160 func (r *Request) GetFormMapStrStr(kvMap ...map[string]interface{}) map[string]string { 161 postMap := r.GetFormMap(kvMap...) 162 if len(postMap) > 0 { 163 m := make(map[string]string, len(postMap)) 164 for k, v := range postMap { 165 m[k] = gconv.String(v) 166 } 167 return m 168 } 169 return nil 170 } 171 172 // GetFormMapStrVar retrieves and returns all form parameters passed from client as map[string]*gvar.Var. 173 // The parameter <kvMap> specifies the keys retrieving from client parameters, the associated values 174 // are the default values if the client does not pass. 175 func (r *Request) GetFormMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var { 176 postMap := r.GetFormMap(kvMap...) 177 if len(postMap) > 0 { 178 m := make(map[string]*gvar.Var, len(postMap)) 179 for k, v := range postMap { 180 m[k] = gvar.New(v) 181 } 182 return m 183 } 184 return nil 185 } 186 187 // GetFormStruct retrieves all form parameters passed from client and converts them to 188 // given struct object. Note that the parameter <pointer> is a pointer to the struct object. 189 // The optional parameter <mapping> is used to specify the key to attribute mapping. 190 func (r *Request) GetFormStruct(pointer interface{}, mapping ...map[string]string) error { 191 _, err := r.doGetFormStruct(pointer, mapping...) 192 return err 193 } 194 195 func (r *Request) doGetFormStruct(pointer interface{}, mapping ...map[string]string) (data map[string]interface{}, err error) { 196 r.parseForm() 197 data = r.formMap 198 if data == nil { 199 data = map[string]interface{}{} 200 } 201 if err := r.mergeDefaultStructValue(data, pointer); err != nil { 202 return data, nil 203 } 204 return data, gconv.Struct(data, pointer, mapping...) 205 }