forms

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ModelFormSetFactory

func ModelFormSetFactory(modelInstance any, includes, excludes []string, extra int) func() *BaseFormSet

ModelFormSetFactory generates a factory for ModelFormSets. In Django this returns a class. Here it returns a configured BaseFormSet initialization function.

Types

type BaseField

type BaseField struct {
	WidgetField Widget
	IsRequired  bool
	LabelStr    string
	HelpTextStr string
}

func (*BaseField) HelpText

func (f *BaseField) HelpText() string

func (*BaseField) Label

func (f *BaseField) Label() string

func (*BaseField) Required

func (f *BaseField) Required() bool

func (*BaseField) SetWidget

func (f *BaseField) SetWidget(w Widget)

func (*BaseField) Widget

func (f *BaseField) Widget() Widget

type BaseFormSet

type BaseFormSet struct {
	FormFactory func() *Form // Factory to create the forms
	Forms       []*Form      // The instantiated forms
	Data        map[string]any
	Files       map[string][]*multipart.FileHeader
	Prefix      string

	TotalFormCount   int
	InitialFormCount int
	MaxNum           int

	Errors  []FormErrors
	IsBound bool
}

BaseFormSet manages a collection of instances of a specific form.

func NewBaseFormSet

func NewBaseFormSet(factory func() *Form, prefix string, total, initial, maxNum int) *BaseFormSet

NewBaseFormSet initializes a BaseFormSet.

func (*BaseFormSet) Bind

func (fs *BaseFormSet) Bind(data map[string]any, files map[string][]*multipart.FileHeader)

Bind assigns data to all forms in the set.

func (*BaseFormSet) IsValid

func (fs *BaseFormSet) IsValid() bool

IsValid runs validation on all forms.

type BaseWidget

type BaseWidget struct {
	Attrs map[string]string
	Media *Media
}

func (*BaseWidget) GetMedia

func (w *BaseWidget) GetMedia() *Media

type BooleanField

type BooleanField struct {
	BaseField
}

BooleanField

func (*BooleanField) Clean

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

type CharField

type CharField struct {
	BaseField
	MaxLength int
	MinLength int
	Strip     bool
}

CharField

func (*CharField) Clean

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

type CheckboxInput

type CheckboxInput struct {
	BaseWidget
}

CheckboxInput is `<input type="checkbox">`.

func NewCheckboxInput

func NewCheckboxInput(attrs map[string]string) *CheckboxInput

func (*CheckboxInput) Render

func (w *CheckboxInput) Render(name string, value any, attrs map[string]string) template.SafeString

type CheckboxSelectMultiple

type CheckboxSelectMultiple struct {
	BaseWidget
	Choices []Choice
}

CheckboxSelectMultiple renders a list of checkboxes.

func NewCheckboxSelectMultiple

func NewCheckboxSelectMultiple(choices []Choice, attrs map[string]string) *CheckboxSelectMultiple

func (*CheckboxSelectMultiple) Render

func (w *CheckboxSelectMultiple) Render(name string, value any, attrs map[string]string) template.SafeString

type Choice

type Choice struct {
	Value string
	Label string
}

type ChoiceField

type ChoiceField struct {
	BaseField
	Choices []Choice
}

ChoiceField

func (*ChoiceField) Clean

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

type DateField

type DateField struct {
	BaseField
}

DateField (Simplified parsing)

func (*DateField) Clean

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

type EmailField

type EmailField struct {
	CharField
}

EmailField

func (*EmailField) Clean

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

type Field

type Field interface {
	Clean(value any) (any, error)
	Widget() Widget
	Required() bool
	Label() string
	HelpText() string
	SetWidget(w Widget)
}

Field interface handles rendering and validation of a form field.

type FileField

type FileField struct {
	BaseField
	MaxBytes     int64
	AllowedTypes []string
}

FileField and ImageField stubs

func (*FileField) Clean

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

type FloatField

type FloatField struct {
	BaseField
}

FloatField

func (*FloatField) Clean

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

type Form

type Form struct {
	Fields      map[string]Field
	Data        map[string]any
	Files       map[string][]*multipart.FileHeader
	CleanedData map[string]any
	Errors      FormErrors

	// Ordered fields for rendering
	Order []string

	IsBound bool
}

Form represents a collection of fields and their bound data.

func NewForm

func NewForm(fields map[string]Field, order []string) *Form

NewForm initializes an empty form.

func (*Form) Bind

func (f *Form) Bind(data map[string]any, files map[string][]*multipart.FileHeader)

Bind assigns data and files to the form for validation.

func (*Form) Clean

func (f *Form) Clean() error

Clean can be overridden to implement cross-field validation.

func (*Form) IsValid

func (f *Form) IsValid() bool

IsValid runs all field validation and form-level validation.

func (*Form) Media

func (f *Form) Media() *Media

Media returns all unique CSS and JS paths required by the form's widgets.

func (*Form) NonFieldErrors

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

NonFieldErrors returns errors not associated with a specific field.

func (*Form) Render

func (f *Form) Render() template.SafeString

Render is a shortcut mapping to RenderTable.

func (*Form) RenderTable

func (f *Form) RenderTable() template.SafeString

Render represents the form as a table structure.

type FormErrors

type FormErrors map[string][]string

FormErrors maps field names to lists of error messages.

type ImageField

type ImageField struct {
	FileField
}

func (*ImageField) Clean

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

type Input

type Input struct {
	BaseWidget
	InputType string
}

Input is a generic `<input type="...">` widget.

func NewClearableFileInput

func NewClearableFileInput(attrs map[string]string) *Input

ClearableFileInput extends FileInput allowing clearing the selection (simplified).

func NewDateInput

func NewDateInput(attrs map[string]string) *Input

DateInput is `<input type="date">`.

func NewDateTimeInput

func NewDateTimeInput(attrs map[string]string) *Input

DateTimeInput is `<input type="datetime-local">`.

func NewEmailInput

func NewEmailInput(attrs map[string]string) *Input

EmailInput is `<input type="email">`.

func NewFileInput

func NewFileInput(attrs map[string]string) *Input

FileInput is `<input type="file">`.

func NewHiddenInput

func NewHiddenInput(attrs map[string]string) *Input

HiddenInput is `<input type="hidden">`.

func NewNumberInput

func NewNumberInput(attrs map[string]string) *Input

NumberInput is `<input type="number">`.

func NewPasswordInput

func NewPasswordInput(attrs map[string]string) *Input

PasswordInput is `<input type="password">`.

func NewTextInput

func NewTextInput(attrs map[string]string) *Input

TextInput is `<input type="text">`.

func NewTimeInput

func NewTimeInput(attrs map[string]string) *Input

TimeInput is `<input type="time">`.

func NewURLInput

func NewURLInput(attrs map[string]string) *Input

URLInput is `<input type="url">`.

func (*Input) Render

func (w *Input) Render(name string, value any, attrs map[string]string) template.SafeString

type IntegerField

type IntegerField struct {
	BaseField
}

IntegerField

func (*IntegerField) Clean

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

type Media

type Media struct {
	CSS map[string][]string // Keyed by medium, e.g., "all", "screen"
	JS  []string
}

Media handles CSS and JS files for a widget or form.

func NewMedia

func NewMedia() *Media

NewMedia creates an empty Media object.

func (*Media) Merge

func (m *Media) Merge(other *Media)

Merge combines two Media objects, deduplicating paths.

func (*Media) Render

func (m *Media) Render() template.SafeString

Render outputs all tags.

func (*Media) RenderCSS

func (m *Media) RenderCSS() template.SafeString

RenderCSS outputs <link> tags.

func (*Media) RenderJS

func (m *Media) RenderJS() template.SafeString

RenderJS outputs <script> tags.

type ModelForm

type ModelForm struct {
	Form
	ModelType reflect.Type
	Instance  any
	Includes  []string
	Excludes  []string
}

ModelForm represents a form automatically generated from an ORM model.

func NewModelForm

func NewModelForm(modelInstance any, includes, excludes []string) (*ModelForm, error)

NewModelForm generates a new ModelForm based on the specified model struct instance.

func (*ModelForm) Save

func (mf *ModelForm) Save(commit bool) (any, error)

Save commits the CleanedData to the underlying Model instance. If commit=true, it would theoretically call the ORM to save to the database.

type MultipleChoiceField

type MultipleChoiceField struct {
	BaseField
	Choices []Choice
}

MultipleChoiceField

func (*MultipleChoiceField) Clean

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

type RadioSelect

type RadioSelect struct {
	CheckboxSelectMultiple // Logic is very similar
}

RadioSelect renders a list of radio buttons.

func NewRadioSelect

func NewRadioSelect(choices []Choice, attrs map[string]string) *RadioSelect

func (*RadioSelect) Render

func (w *RadioSelect) Render(name string, value any, attrs map[string]string) template.SafeString

type Select

type Select struct {
	BaseWidget
	Choices []Choice
}

Select is `<select>...<option>...</select>`.

func NewNullBooleanSelect

func NewNullBooleanSelect(attrs map[string]string) *Select

NullBooleanSelect is a Select with Yes/No/Unknown.

func NewSelect

func NewSelect(choices []Choice, attrs map[string]string) *Select

func (*Select) Render

func (w *Select) Render(name string, value any, attrs map[string]string) template.SafeString

type SelectMultiple

type SelectMultiple struct {
	Select
}

SelectMultiple is `<select multiple>`.

func NewSelectMultiple

func NewSelectMultiple(choices []Choice, attrs map[string]string) *SelectMultiple

type SlugField

type SlugField struct {
	CharField
}

SlugField

func (*SlugField) Clean

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

type SplitDateTimeWidget

type SplitDateTimeWidget struct {
	BaseWidget
	DateWidget Widget
	TimeWidget Widget
}

SplitDateTimeWidget renders date and time separately.

func NewSplitDateTimeWidget

func NewSplitDateTimeWidget(attrs map[string]string) *SplitDateTimeWidget

func (*SplitDateTimeWidget) Render

func (w *SplitDateTimeWidget) Render(name string, value any, attrs map[string]string) template.SafeString

type Textarea

type Textarea struct {
	BaseWidget
}

Textarea is `<textarea>...</textarea>`.

func NewTextarea

func NewTextarea(attrs map[string]string) *Textarea

func (*Textarea) Render

func (w *Textarea) Render(name string, value any, attrs map[string]string) template.SafeString

type URLField

type URLField struct {
	CharField
}

URLField

func (*URLField) Clean

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

type Widget

type Widget interface {
	Render(name string, value any, attrs map[string]string) template.SafeString
	GetMedia() *Media
}

Widget interface defining the rendering of a form field.

Jump to

Keyboard shortcuts

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