FormValueOr extracts and parses a single form value from the request by key. Returns defaultValue if the parameter is not found or has an empty value. Returns an error only if parsing fails or form parsing errors occur. Example: limit, err := echo.FormValueOr[int](c, "limit", 100) // If "limit"
(c *Context, key string, defaultValue T, opts ...any)
| 246 | // |
| 247 | // See ParseValue for supported types and options |
| 248 | func FormValueOr[T any](c *Context, key string, defaultValue T, opts ...any) (T, error) { |
| 249 | formValues, err := c.FormValues() |
| 250 | if err != nil { |
| 251 | var zero T |
| 252 | return zero, fmt.Errorf("failed to parse form value, key: %s, err: %w", key, err) |
| 253 | } |
| 254 | values, ok := formValues[key] |
| 255 | if !ok { |
| 256 | return defaultValue, nil |
| 257 | } |
| 258 | if len(values) == 0 { |
| 259 | return defaultValue, nil |
| 260 | } |
| 261 | value := values[0] |
| 262 | v, err := ParseValueOr[T](value, defaultValue, opts...) |
| 263 | if err != nil { |
| 264 | return v, NewBindingError(key, []string{value}, "form value", err) |
| 265 | } |
| 266 | return v, nil |
| 267 | } |
| 268 | |
| 269 | // FormValues extracts and parses all values for a form values key as a slice. |
| 270 | // It returns the typed slice and an error if binding any value fails. Returns ErrNonExistentKey if parameter not found. |
nothing calls this directly
no test coverage detected
searching dependent graphs…