forms

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrValidation = errors.New("form validation")

ErrValidation indicates form field validation failed.

Functions

This section is empty.

Types

type Attrs

type Attrs map[string]string

Attrs contains HTML attributes applied to a widget render call.

type BoundField

type BoundField struct {
	Form  *Form
	Name  string
	Field *Field
}

BoundField is one field bound to a form instance.

func (BoundField) Errors

func (bf BoundField) Errors() ErrorList

func (BoundField) HTMLName

func (bf BoundField) HTMLName() string

func (BoundField) IsHidden

func (bf BoundField) IsHidden() bool

func (BoundField) Label

func (bf BoundField) Label() string

func (BoundField) LabelTag

func (bf BoundField) LabelTag() string

func (BoundField) Render

func (bf BoundField) Render() string

func (BoundField) Value

func (bf BoundField) Value() any

type BoundFieldGroup

type BoundFieldGroup struct {
	Name   string
	Fields []BoundField
}

BoundFieldGroup is a field group resolved to bound fields.

type BoundForm

type BoundForm struct {
	Index int
	Form  *Form
}

BoundForm stores a form and its formset index.

type Choice

type Choice struct {
	Value any
	Label string
}

Choice is one selectable form value.

type ErrorDict

type ErrorDict map[string]ErrorList

ErrorDict stores field-specific validation errors.

func (ErrorDict) Add

func (d ErrorDict) Add(field string, err error)

func (ErrorDict) Get

func (d ErrorDict) Get(field string) ErrorList

func (ErrorDict) HasErrors

func (d ErrorDict) HasErrors() bool

type ErrorList

type ErrorList []ValidationError

ErrorList is an ordered list of validation errors.

func (ErrorList) HTML

func (l ErrorList) HTML() string

func (ErrorList) Messages

func (l ErrorList) Messages() []string

type Field

type Field struct {
	Kind       string
	Options    FieldOptions
	Choices    []Choice
	Fields     []*Field
	Regex      *regexp.Regexp
	Coerce     func(string) (any, error)
	CleanFunc  func(any) (any, error)
	Compress   func([]any) (any, error)
	EmptyValue any
}

Field parses and validates one form value.

func BooleanField

func BooleanField(options FieldOptions) *Field

func CharField

func CharField(options FieldOptions) *Field

func ChoiceField

func ChoiceField(options FieldOptions, choices []Choice) *Field

func ComboField

func ComboField(options FieldOptions, fields ...*Field) *Field

func DateField

func DateField(options FieldOptions) *Field

func DateTimeField

func DateTimeField(options FieldOptions) *Field

func DecimalField

func DecimalField(options FieldOptions) *Field

func DurationField

func DurationField(options FieldOptions) *Field

func EmailField

func EmailField(options FieldOptions) *Field

func FileField

func FileField(options FieldOptions) *Field

func FloatField

func FloatField(options FieldOptions) *Field

func GenericIPAddressField

func GenericIPAddressField(options FieldOptions) *Field

func ImageField

func ImageField(options FieldOptions) *Field

func IntegerField

func IntegerField(options FieldOptions) *Field

func JSONField

func JSONField(options FieldOptions) *Field

func ModelChoiceField

func ModelChoiceField(options FieldOptions, choices []Choice) *Field

func ModelMultipleChoiceField

func ModelMultipleChoiceField(options FieldOptions, choices []Choice) *Field

func MultiValueField

func MultiValueField(options FieldOptions, fields ...*Field) *Field

func MultipleChoiceField

func MultipleChoiceField(options FieldOptions, choices []Choice) *Field

func MultipleFileField

func MultipleFileField(options FieldOptions) *Field

func RegexField

func RegexField(options FieldOptions, pattern *regexp.Regexp) *Field

func SlugField

func SlugField(options FieldOptions) *Field

func SplitDateTimeField

func SplitDateTimeField(options FieldOptions) *Field

func TimeField

func TimeField(options FieldOptions) *Field

func TypedChoiceField

func TypedChoiceField(options FieldOptions, choices []Choice, coerce func(string) (any, error)) *Field

func URLField

func URLField(options FieldOptions) *Field

func UUIDField

func UUIDField(options FieldOptions) *Field

func (*Field) Clean

func (f *Field) Clean(value any) (any, error)

Clean parses, validates, and returns a cleaned field value.

type FieldGroup

type FieldGroup struct {
	Name   string
	Fields []string
}

FieldGroup defines a named group of form fields.

type FieldOptions

type FieldOptions struct {
	Required      bool
	Label         string
	Initial       any
	HelpText      string
	Validators    []Validator
	Disabled      bool
	Localize      bool
	ErrorMessages map[string]string
	Widget        any
}

FieldOptions configures common form field behavior.

type Form

type Form struct {
	CleanedData map[string]any
	// contains filtered or unexported fields
}

Form binds data to fields and coordinates validation and rendering.

func NewForm

func NewForm(options FormOptions) *Form

func (*Form) AddError

func (f *Form) AddError(field string, err error)

func (*Form) AddNonFieldError

func (f *Form) AddNonFieldError(err error)

func (*Form) BoundField

func (f *Form) BoundField(name string) BoundField

func (*Form) ChangedData

func (f *Form) ChangedData() []string

func (*Form) Errors

func (f *Form) Errors() ErrorDict

func (*Form) FieldErrors

func (f *Form) FieldErrors(name string) ErrorList

func (*Form) FieldGroups

func (f *Form) FieldGroups() []BoundFieldGroup

func (*Form) FieldNames

func (f *Form) FieldNames() []string

func (*Form) FullClean

func (f *Form) FullClean()

func (*Form) HiddenFields

func (f *Form) HiddenFields() []BoundField

func (*Form) IsBound

func (f *Form) IsBound() bool

func (*Form) IsValid

func (f *Form) IsValid() bool

func (*Form) Media

func (f *Form) Media() Media

func (*Form) NonFieldErrors

func (f *Form) NonFieldErrors() NonFieldErrorList

func (*Form) Render

func (f *Form) Render() string

func (*Form) RenderErrors

func (f *Form) RenderErrors() string

func (*Form) VisibleFields

func (f *Form) VisibleFields() []BoundField

type FormCleanFunc

type FormCleanFunc func(*Form) error

FormCleanFunc validates cross-field state after individual fields clean.

type FormOptions

type FormOptions struct {
	Fields     map[string]*Field
	FieldOrder []string
	Data       map[string]any
	Initial    map[string]any
	Prefix     string
	Bound      bool
	Groups     []FieldGroup
	Clean      FormCleanFunc
}

FormOptions configures a form instance.

type FormSet

type FormSet struct {
	// contains filtered or unexported fields
}

FormSet coordinates repeated instances of one form.

func NewFormSet

func NewFormSet(options FormSetOptions) *FormSet

func (*FormSet) DeletedForms

func (fs *FormSet) DeletedForms() []BoundForm

func (*FormSet) EmptyForms

func (fs *FormSet) EmptyForms() []BoundForm

func (*FormSet) Forms

func (fs *FormSet) Forms() []BoundForm

func (*FormSet) InitialForms

func (fs *FormSet) InitialForms() []BoundForm

func (*FormSet) IsValid

func (fs *FormSet) IsValid() bool

func (*FormSet) ManagementForm

func (fs *FormSet) ManagementForm() ManagementForm

func (*FormSet) NewForms

func (fs *FormSet) NewForms() []BoundForm

func (*FormSet) NonFormErrors

func (fs *FormSet) NonFormErrors() ErrorList

func (*FormSet) OrderedForms

func (fs *FormSet) OrderedForms() []BoundForm

type FormSetFormFactory

type FormSetFormFactory func(FormSetFormOptions) *Form

FormSetFormFactory builds one form for an index.

type FormSetFormOptions

type FormSetFormOptions struct {
	Index   int
	Prefix  string
	Data    map[string]any
	Initial map[string]any
}

FormSetFormOptions configures one form inside a formset.

type FormSetOptions

type FormSetOptions struct {
	Prefix      string
	Data        map[string]any
	Initial     []map[string]any
	Extra       int
	MinForms    int
	MaxForms    int
	CanDelete   bool
	CanOrder    bool
	FormFactory FormSetFormFactory
}

FormSetOptions configures a Django-style formset.

type InlineFormSet

type InlineFormSet struct {
	*FormSet

	Parent        models.Model
	RelationField string
	ModelForms    []*ModelForm
	Store         models.InstanceStore
	Context       context.Context
	SaveOptions   []models.SaveOption
}

InlineFormSet stores a formset of child model forms.

func NewInlineFormSet

func NewInlineFormSet(options InlineFormSetOptions) *InlineFormSet

func (*InlineFormSet) Save

func (fs *InlineFormSet) Save(commit bool) ([]models.Model, error)

type InlineFormSetOptions

type InlineFormSetOptions struct {
	FormSetOptions
	Parent        models.Model
	RelationField string
	ModelFactory  func(index int) models.Model
	ModelFields   []modelfields.Field
	Include       []string
	Exclude       []string
	Labels        map[string]string
	HelpTexts     map[string]string
	Widgets       map[string]Widget
	FieldClasses  map[string]ModelFieldFactory
	Store         models.InstanceStore
	Context       context.Context
	SaveOptions   []models.SaveOption
}

InlineFormSetOptions configures model forms tied to one parent model.

type ManagementForm

type ManagementForm struct {
	TotalForms   int
	InitialForms int
	MinForms     int
	MaxForms     int
}

ManagementForm stores parsed formset management counters.

type Media

type Media struct {
	CSS []string
	JS  []string
}

Media contains static assets required by a form or widget.

func (Media) Merge

func (m Media) Merge(other Media) Media

type MediaProvider

type MediaProvider interface {
	Media() Media
}

MediaProvider is implemented by widgets that need CSS or JavaScript assets.

type ModelFieldFactory

type ModelFieldFactory func(modelfields.Field, FieldOptions) *Field

ModelFieldFactory overrides the generated form field for one model field.

type ModelForm

type ModelForm struct {
	*Form

	Model       models.Model
	Meta        models.Metadata
	Store       models.InstanceStore
	Context     context.Context
	SaveOptions []models.SaveOption
	// contains filtered or unexported fields
}

ModelForm binds a Form to a model instance.

func NewModelForm

func NewModelForm(options ModelFormOptions) *ModelForm

func (*ModelForm) IsValid

func (mf *ModelForm) IsValid() bool

func (*ModelForm) Save

func (mf *ModelForm) Save(commit bool, options ...models.SaveOption) (models.Model, error)

type ModelFormOptions

type ModelFormOptions struct {
	Model           models.Model
	Meta            models.Metadata
	ModelFields     []modelfields.Field
	Include         []string
	Exclude         []string
	Labels          map[string]string
	HelpTexts       map[string]string
	Widgets         map[string]Widget
	FieldClasses    map[string]ModelFieldFactory
	LocalizedFields []string
	ReadOnly        []string
	Data            map[string]any
	Initial         map[string]any
	Prefix          string
	Groups          []FieldGroup
	Clean           FormCleanFunc
	Store           models.InstanceStore
	Context         context.Context
	SaveOptions     []models.SaveOption
}

ModelFormOptions configures a model-backed form.

type NonFieldErrorList

type NonFieldErrorList ErrorList

NonFieldErrorList renders form-level errors with a distinct CSS class.

func (NonFieldErrorList) HTML

func (l NonFieldErrorList) HTML() string

func (NonFieldErrorList) Messages

func (l NonFieldErrorList) Messages() []string

type UploadedFile

type UploadedFile struct {
	Name        string
	Size        int64
	ContentType string
	Content     []byte
}

UploadedFile is a form-level uploaded file value.

type ValidationError

type ValidationError struct {
	Code    string
	Message string
}

ValidationError is a user-facing form validation error.

func (ValidationError) Error

func (e ValidationError) Error() string

type Validator

type Validator func(any) error

Validator validates one cleaned field value.

type Widget

type Widget interface {
	Render(name string, value any, attrs Attrs) string
}

Widget renders one form value into HTML.

func CheckboxInput

func CheckboxInput() Widget

func CheckboxSelectMultiple

func CheckboxSelectMultiple(choices []Choice) Widget

func ClearableFileInput

func ClearableFileInput() Widget

func DateInput

func DateInput() Widget

func DateTimeInput

func DateTimeInput() Widget

func EmailInput

func EmailInput() Widget

func FileInput

func FileInput() Widget

func HiddenInput

func HiddenInput() Widget

func MultipleHiddenInput

func MultipleHiddenInput() Widget

func NumberInput

func NumberInput() Widget

func PasswordInput

func PasswordInput() Widget

func RadioSelect

func RadioSelect(choices []Choice) Widget

func Select

func Select(choices []Choice) Widget

func SelectMultiple

func SelectMultiple(choices []Choice) Widget

func SplitDateTimeWidget

func SplitDateTimeWidget() Widget

func TextInput

func TextInput() Widget

func Textarea

func Textarea() Widget

func TimeInput

func TimeInput() Widget

func URLInput

func URLInput() Widget

Jump to

Keyboard shortcuts

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