forms

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: EUPL-1.2 Imports: 9 Imported by: 0

Documentation

Overview

Package forms provides Django-style form structs 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   // from verbose_name/verbose tag
	HelpText string   // from help_text tag
	Type     string   // "text", "number", "textarea", "select", "checkbox", "date", "email", "hidden"
	Value    any      // current value
	Required bool     // from validate:"required"
	Choices  []Choice // static or dynamic
	Errors   []string // field-specific error messages
}

BoundField provides field metadata for template rendering.

type Choice

type Choice struct {
	Value    string
	Label    string
	LabelKey string // optional i18n key
}

Choice represents a single option in a select or radio field.

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() error
}

Cleanable may be implemented by form structs for cross-field validation. Clean is called after per-field validation passes. 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).

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 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.

Jump to

Keyboard shortcuts

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