Documentation
¶
Overview ¶
Package forms provides Django-style form structs for creation, binding, validation, and field metadata extraction.
Index ¶
- type BoundField
- type Choice
- type ChoiceProvider
- type Cleanable
- type Form
- func (f *Form[T]) Bind(r *http.Request) bool
- func (f *Form[T]) Errors() *burrow.ValidationError
- func (f *Form[T]) Field(name string) (BoundField, bool)
- func (f *Form[T]) Fields() []BoundField
- func (f *Form[T]) Instance() *T
- func (f *Form[T]) IsValid() bool
- func (f *Form[T]) NonFieldErrors() []string
- type Option
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 ChoiceProvider ¶
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 ¶
FromModel creates a form pre-populated from an existing model instance. If instance is nil, creates an empty form (for create mode).
func (*Form[T]) Bind ¶
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]) NonFieldErrors ¶
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 ¶
WithChoices sets static choices for a field (by Go struct field name).
func WithChoicesFunc ¶
WithChoicesFunc sets a dynamic choice provider for a field (by Go struct field name).
func WithExclude ¶
WithExclude excludes named fields (by Go struct field name) from the form.