forms

package
v0.30.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package forms provides struct-based form definitions for creation, binding, validation, and field metadata extraction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoundField

type BoundField struct {
	Name      string       // Go struct field name
	FormName  string       // HTML field name (from form tag or lowercase)
	Label     string       // translated from verbose_name/verbose tag
	HelpText  string       // from help_text tag
	Type      string       // "text", "number", "textarea", "select", "checkbox", "date", "email", "hidden", "subform"
	Value     any          // current value
	Required  bool         // from validate:"required"
	ReadOnly  bool         // render as plain text, not editable
	Choices   []Choice     // static or dynamic, with translated labels
	SubFields []BoundField // populated when Type == "subform"
	Errors    []string     // field-specific error messages
}

BoundField provides field metadata for template rendering.

Label is read from the verbose_name/verbose struct tag and piped through i18n.T by [extractFields], so the English Label doubles as the i18n message ID. Templates render {{ .Label }} as-is — no {{ t }} wrapping needed. See docs/guide/i18n.md for the Label-as-key convention.

When Type is "subform", SubFields holds the nested struct's BoundFields (one level of recursion). Their FormName uses the parent.child convention (e.g. "profile.name"), which matches the burrow.Bind decoder.

type Choice

type Choice struct {
	Value string
	Label string
}

Choice represents a single option in a select or radio field. Label is piped through i18n.T by [extractFields], following the same Label-as-key convention as BoundField.Label.

type ChoiceProvider

type ChoiceProvider interface {
	FieldChoices(ctx context.Context, field string) ([]Choice, error)
}

ChoiceProvider may be implemented by form structs that provide dynamic choices for select fields. The field parameter is the Go struct field name.

type Cleanable

type Cleanable interface {
	Clean(ctx context.Context) error
}

Cleanable may be implemented by form structs for cross-field validation. Clean is called after per-field validation passes. The context carries request-scoped data (e.g. i18n localizer). It may return a *burrow.ValidationError to report field-level or non-field errors.

type Form

type Form[T any] struct {
	// contains filtered or unexported fields
}

Form holds form state for a struct type T.

func FromModel

func FromModel[T any](instance *T, opts ...Option[T]) *Form[T]

FromModel creates a form pre-populated from an existing model instance. If instance is nil, creates an empty form (for create mode).

func New

func New[T any](opts ...Option[T]) *Form[T]

New creates an empty form for type T.

func (*Form[T]) Bind

func (f *Form[T]) Bind(r *http.Request) bool

Bind decodes the request body into the form struct, validates it, and runs any Cleanable.Clean method. Returns true if the form is valid.

func (*Form[T]) Errors

func (f *Form[T]) Errors() *burrow.ValidationError

Errors returns the validation errors, or nil if valid.

func (*Form[T]) Field

func (f *Form[T]) Field(name string) (BoundField, bool)

Field returns a single BoundField by Go struct field name.

func (*Form[T]) Fields

func (f *Form[T]) Fields() []BoundField

Fields returns all visible BoundFields in struct field order. Validation errors are auto-translated via i18n.TData.

func (*Form[T]) Instance

func (f *Form[T]) Instance() *T

Instance returns the bound/populated struct.

func (*Form[T]) IsValid

func (f *Form[T]) IsValid() bool

IsValid reports whether the form passed validation.

func (*Form[T]) NonFieldErrors

func (f *Form[T]) NonFieldErrors() []string

NonFieldErrors returns errors not tied to a specific field (from Clean).

func (*Form[T]) WithContext added in v0.22.0

func (f *Form[T]) WithContext(ctx context.Context) *Form[T]

WithContext sets the context used when rendering the form's fields. Bind already does this from the http.Request, so call WithContext only when rendering a form outside the request lifecycle (background jobs, CLI, templates rendered from a goroutine). The context's locale drives label/choice translation in Fields.

type Option

type Option[T any] func(*formConfig[T])

Option configures a Form during construction.

func WithChoices

func WithChoices[T any](field string, choices []Choice) Option[T]

WithChoices sets static choices for a field (by Go struct field name).

func WithChoicesFunc

func WithChoicesFunc[T any](field string, fn func(context.Context) ([]Choice, error)) Option[T]

WithChoicesFunc sets a dynamic choice provider for a field (by Go struct field name).

func WithCleanFunc added in v0.5.0

func WithCleanFunc[T any](fn func(context.Context, *T) error) Option[T]

WithCleanFunc sets a closure-based clean function for cross-field validation that needs external dependencies (e.g. database access). The function runs after per-field validation and Clean() (if implemented). It may return a *burrow.ValidationError for field-level errors or a plain error for non-field errors.

func WithExclude

func WithExclude[T any](fields ...string) Option[T]

WithExclude excludes named fields (by Go struct field name) from the form.

func WithInitial

func WithInitial[T any](values map[string]any) Option[T]

WithInitial sets initial field values by form name.

func WithReadOnly added in v0.5.0

func WithReadOnly[T any](fields ...string) Option[T]

WithReadOnly marks named fields (by Go struct field name) as read-only. Read-only fields are rendered as plain text instead of input elements.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL