FormValue extracts and parses a single form value from the request by key. It returns the typed value and an error if binding fails. Returns ErrNonExistentKey if parameter not found. Empty String Handling: If the form field exists but has an empty value, the zero value of type T is returned with
(c *Context, key string, opts ...any)
| 211 | // |
| 212 | // See ParseValue for supported types and options |
| 213 | func FormValue[T any](c *Context, key string, opts ...any) (T, error) { |
| 214 | formValues, err := c.FormValues() |
| 215 | if err != nil { |
| 216 | var zero T |
| 217 | return zero, fmt.Errorf("failed to parse form value, key: %s, err: %w", key, err) |
| 218 | } |
| 219 | values, ok := formValues[key] |
| 220 | if !ok { |
| 221 | var zero T |
| 222 | return zero, ErrNonExistentKey |
| 223 | } |
| 224 | if len(values) == 0 { |
| 225 | var zero T |
| 226 | return zero, nil |
| 227 | } |
| 228 | value := values[0] |
| 229 | v, err := ParseValue[T](value, opts...) |
| 230 | if err != nil { |
| 231 | return v, NewBindingError(key, []string{value}, "form value", err) |
| 232 | } |
| 233 | return v, nil |
| 234 | } |
| 235 | |
| 236 | // FormValueOr extracts and parses a single form value from the request by key. |
| 237 | // Returns defaultValue if the parameter is not found or has an empty value. |
searching dependent graphs…