kataras/iris · warning

ErrEmptyFormField

ErrEmptyFormField

Error message

%w: %s

What it means

This error is returned by Context.PostValues-family lookup helpers (e.g. ctx.PostValueMany/PostValues) when a form field exists in the POST form data but its first value is empty or whitespace-only after trimming. The library wraps the sentinel ErrEmptyFormField and appends the offending field name so callers can distinguish 'field missing' (ErrNotFound) from 'field present but empty'.

Source

Thrown at context/context.go:1912

func (ctx *Context) PostValues(name string) ([]string, error) {
	_, ok := ctx.form()
	if !ok {
		if !ctx.app.ConfigurationReadOnly().GetFireEmptyFormError() {
			return nil, nil
		}

		return nil, ErrEmptyForm // empty form.
	}

	values, ok := ctx.request.PostForm[name]
	if !ok {
		return nil, ErrNotFound // field does not exist
	}

	if len(values) == 0 ||
		// Fast check for its first empty value (see below).
		strings.TrimSpace(values[0]) == "" {
		return nil, fmt.Errorf("%w: %s", ErrEmptyFormField, name)
	}

	for _, value := range values {
		if value == "" { // if at least one empty value, then perform the strip from the beginning.
			result := make([]string, 0, len(values))
			for _, value := range values {
				if strings.TrimSpace(value) != "" {
					result = append(result, value) // we store the value as it is, not space-trimmed.
				}
			}

			if len(result) == 0 {
				return nil, fmt.Errorf("%w: %s", ErrEmptyFormField, name)
			}

			return result, nil
		}
	}

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Check errors.Is(err, iris/context.ErrEmptyFormField) and treat it as a missing optional field rather than a hard failure.
  2. Have the client omit the key entirely or send a real value; validate before submit.
  3. Use ctx.PostValues and decide per-value semantics yourself if empty strings are meaningful.
  4. For required fields, return a 400 with the field name extracted from the wrapped error message.

Example fix

// before
vals, err := ctx.PostValues("tags")
if err != nil { return err }
// after
vals, err := ctx.PostValues("tags")
if errors.Is(err, context.ErrEmptyFormField) {
    vals = nil // treat empty as absent
} else if err != nil {
    return err
}
Defensive patterns

Strategy: validation

Validate before calling

if name := ctx.FormValue("tags"); name == "" { /* treat as absent before calling PostValues */ }

Type guard

func isEmptyFormField(err error) bool { return errors.Is(err, context.ErrEmptyFormField) }

Try / catch

vals, err := ctx.PostValues("tags")
switch {
case errors.Is(err, context.ErrEmptyFormField):
    vals = nil
case err != nil:
    return err
}

Prevention

When it happens

Trigger: Calling ctx.PostValue(name)/PostValueMany/PostValueTrim where the multipart or urlencoded form contains the key but values[0] trims to "" (e.g. input left blank in the browser).

Common situations: HTML forms with optional text inputs submitted empty; automated clients sending 'field=' pairs; mobile apps posting empty strings instead of omitting keys.

Related errors


AI-assisted analysis of kataras/iris@7bedaf55a0 (2026-08-30). Data as JSON: /api/errors/d92b5fa2d6caaf0b. Report an issue: GitHub.