MCPcopy Index your code
hub / github.com/labstack/echo / FormValueOr

Function FormValueOr

binder_generic.go:248–267  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

246//
247// See ParseValue for supported types and options
248func 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.

Callers

nothing calls this directly

Calls 2

NewBindingErrorFunction · 0.85
FormValuesMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…