forms

package
v1.7.4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: GPL-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormValueFromDataDict

func FormValueFromDataDict[T any](ctx context.Context, form FormFieldDefiner, name string, data url.Values, files map[string][]filesystem.FileHeader) (T, bool, []error)

func FullClean

func FullClean(ctx context.Context, f Form) (invalid, defaults, cleaned map[string]any)

func IsValid

func IsValid[T any](ctx context.Context, formObj T) bool

Types

type BinderWidget

type BinderWidget interface {
	Widget
	BoundField(ctx context.Context, w Widget, f Field, name string, value interface{}, errors []error) BoundField
}

type BoundField

type BoundField interface {
	ID() string
	Name() string
	Widget() Widget
	Hidden() bool
	Input() Field
	Label() template.HTML
	HelpText() template.HTML
	Field() template.HTML
	HTML() template.HTML
	Context() context.Context
	Attrs() map[string]string
	Value() interface{}
	Errors() []error
}

type BoundForm

type BoundForm interface {
	AsP() template.HTML
	AsUL() template.HTML
	AsTable() template.HTML
	Media() media.Media
	Fields() []BoundField
	FieldMap() map[string]BoundField // map of field name to BoundField
	ErrorList() []error
	UnpackErrors() []FieldError
	Errors() *orderedmap.OrderedMap[string, []error]
}

type CleanableForm

type CleanableForm interface {
	Clean(ctx context.Context, cleaned map[string]interface{}) (map[string]interface{}, []error)
}

type Cleaner

type Cleaner interface {
	Clean(ctx context.Context, value interface{}) (interface{}, error)
}

type ErrorAdder

type ErrorAdder interface {
	AddFormError(errorList ...error)
	AddError(name string, errorList ...error)
}

type ErrorDefiner

type ErrorDefiner interface {
	ErrorList() []error
	BoundErrors() *orderedmap.OrderedMap[string, []error]
}

type Field

type Field interface {
	FormValueConverter
	Attrs() map[string]string
	SetAttrs(attrs map[string]string)
	Hide(hidden bool)

	SetName(name string)
	SetLabel(label func(ctx context.Context) string)
	SetHelpText(helpText func(ctx context.Context) string)
	SetValidators(validators ...func(interface{}) error)
	SetDefault(defaultValue func() interface{})
	SetWidget(widget Widget)

	Name() string
	Label(ctx context.Context) string
	HelpText(ctx context.Context) string
	Validate(ctx context.Context, value interface{}) []error
	Widget() Widget
	Default() interface{}
	HasChanged(initial, data interface{}) bool

	Clean(ctx context.Context, value interface{}) (interface{}, error)
	Required() bool
	SetRequired(b bool)
	ReadOnly() bool
	SetReadOnly(b bool)
	IsEmpty(value interface{}) bool
}

type FieldError

type FieldError interface {
	Name() string
	Field() string
	Errors() []error
}

type Form

type Form interface {
	WithDataDefiner
	FullCleanMixin
	ErrorAdder
	ErrorDefiner

	Media() media.Media
	Context() context.Context
	WithContext(ctx context.Context)
	Prefix() string
	SetPrefix(prefix string)
	SetInitial(initial map[string]interface{})
	Validators() []func(f Form, cleanedData map[string]interface{}) []error
	SetValidators(validators ...func(Form, map[string]interface{}) []error)
	Renderer() FormRenderer
	SetRenderer(renderer FormRenderer)

	Ordering([]string)
	FieldOrder() []string

	Field(name string) (Field, bool)
	Fields() []Field
	Widgets() []Widget
	AddField(name string, field Field)
	AddWidget(name string, widget Widget)
	DeleteField(name string) bool
	BoundForm() BoundForm
	BoundFields() *orderedmap.OrderedMap[string, BoundField]

	InitialData() map[string]interface{}
	CleanedData() map[string]interface{}

	OnValid(...func(Form))
	OnInvalid(...func(Form))
	OnFinalize(...func(Form))

	WasCleaned() bool
	HasChanged() bool
	CallbackOnValid() []func(f Form)
	CallbackOnInvalid() []func(f Form)
	CallbackOnFinalize() []func(f Form)
	CleanedDataUnsafe() map[string]interface{}
}

type FormFieldDefiner

type FormFieldDefiner interface {
	Field(name string) (Field, bool)
	Widget(name string) (Widget, bool)
	PrefixName(fieldName string) string
}

type FormRenderer

type FormRenderer interface {
	RenderAsP(w io.Writer, ctx context.Context, form BoundForm) error
	RenderAsUL(w io.Writer, ctx context.Context, form BoundForm) error
	RenderAsTable(w io.Writer, ctx context.Context, form BoundForm) error

	RenderFieldLabel(w io.Writer, ctx context.Context, field BoundField, id string, name string) error
	RenderFieldHelpText(w io.Writer, ctx context.Context, field BoundField, id string, name string) error
	RenderFieldWidget(w io.Writer, ctx context.Context, field BoundField, id string, name string, value interface{}, attrs map[string]string, errors []error, widgetCtx ctx.Context) error
	RenderField(w io.Writer, ctx context.Context, field BoundField, id string, name string, value interface{}, errors []error, attrs map[string]string, widgetCtx ctx.Context) error
}

type FormValueConverter

type FormValueConverter interface {
	// Convert the forms' string value to the appropriate GO type.
	ValueToGo(value interface{}) (interface{}, error)

	// Convert the GO type to the forms' string value.
	ValueToForm(value interface{}) interface{}
}

type FormValueGetter

type FormValueGetter interface {
	// Get the value from the provided data.
	ValueFromDataDict(ctx context.Context, data url.Values, files map[string][]filesystem.FileHeader, name string) (interface{}, []error)
}

type FormValueOmitter

type FormValueOmitter interface {
	// Check if the value is omitted from the data provided.
	ValueOmittedFromData(ctx context.Context, data url.Values, files map[string][]filesystem.FileHeader, name string) bool
}

type FormValuer

type FormValuer interface {
	FormValueConverter
	FormValueOmitter
	FormValueGetter
}

type FormWrapper

type FormWrapper[T any] interface {
	Unwrap() []T
}

type FullCleanMixin

type FullCleanMixin interface {
	Widget(name string) (Widget, bool)
	PrefixName(fieldName string) string
	FieldMap() *orderedmap.OrderedMap[string, Field]

	// BindCleanedData might be called multiple times for a single IsValid() call
	BindCleanedData(invalid, defaults, cleaned map[string]interface{})
}

type IsValidChecker

type IsValidChecker[T any] interface {
	CheckIsValid(ctx context.Context, formObj T) bool
}

type IsValidDefiner

type IsValidDefiner interface {
	IsValid() bool
}

type Option

type Option interface {
	Label() string
	Value() string
}

type PrevalidatorMixin

type PrevalidatorMixin interface {
	Prevalidate(ctx context.Context, root any, data url.Values, files map[string][]filesystem.FileHeader) []error
}

type SaveableField

type SaveableField interface {
	Field
	Save(value interface{}) (interface{}, error)
}

type Validator

type Validator interface {
	Validate(ctx context.Context, value interface{}) []error
}

type ValidatorMixin

type ValidatorMixin interface {
	Validators() []func(ctx context.Context, root Form) []error
}

type Widget

type Widget interface {
	IsHidden() bool
	Hide(hidden bool)
	FormType() string
	Field() Field
	BindField(field Field)
	SetAttrs(attrs map[string]string)
	IdForLabel(id string) string
	GetContextData(ctx context.Context, id, name string, value interface{}, attrs map[string]string) ctx.Context
	RenderWithErrors(ctx context.Context, w io.Writer, id, name string, value interface{}, errors []error, attrs map[string]string, context ctx.Context) error

	// Render is basically the same as RenderWithErrors, except that it does not take a context.
	// The widget should always be able to generate some sort of context itself based on the provided parameters.
	Render(ctx context.Context, w io.Writer, id, name string, value interface{}, attrs map[string]string) error
	Validate(ctx context.Context, value interface{}) []error

	FormValuer
	media.MediaDefiner
}

type WithDataDefiner

type WithDataDefiner interface {
	WithData(data url.Values, files map[string][]filesystem.FileHeader, r *http.Request)
	Data() (url.Values, map[string][]filesystem.FileHeader)
}

Jump to

Keyboard shortcuts

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