form

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package form validates and tracks a form's fields: rules per field (required, lengths, email, patterns, matching another field), errors shown after the user leaves a field or tries to submit, a summary of what's wrong, required markers, and whether anything changed since it was loaded or saved.

f := form.New()
f.Input("email", "Email", emailInput, form.Required(), form.Email())
f.Input("password", "Password", pwInput, form.Required(), form.MinLength(8))
f.OnSubmit(func(v map[string]string) { save(v) })

giode.Stack(st,
	f.ErrorSummary(),
	f.Row("email", emailInput),
	f.Row("password", pwInput),
	giode.Button("Save").OnClick(f.Submit),
)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Label

func Label(text string, required bool) elements.Element

Label is a field label, with a required marker.

Types

type Field

type Field struct {
	Name, Label string
	// contains filtered or unexported fields
}

Field is one value of a form.

func (*Field) Dirty

func (fd *Field) Dirty() bool

Dirty reports whether the value changed since it was loaded or saved.

func (*Field) Error

func (fd *Field) Error() string

Error is the field's error, once shown: after the user left the field, or tried to submit.

func (*Field) Focus

func (fd *Field) Focus(fn func()) *Field

Focus sets how to focus the field, for the error summary's links.

func (*Field) Required

func (fd *Field) Required() bool

Required reports whether the field must be filled.

func (*Field) Validate

func (fd *Field) Validate() bool

Validate checks the field and shows its error; it reports whether it's valid.

type FieldError

type FieldError struct{ Name, Label, Message string }

FieldError is a field's error, for a custom summary.

type Form

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

Form is a set of fields.

func New

func New() *Form

New returns an empty form.

func (*Form) Dirty

func (f *Form) Dirty() bool

Dirty reports whether any field changed since the form was loaded or saved: to ask before leaving with unsaved changes, or to enable Save.

func (*Form) DirtyFields

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

DirtyFields are the names of the changed fields.

func (*Form) ErrorSummary

func (f *Form) ErrorSummary() elements.Element

ErrorSummary lists what's wrong after a submit attempt, each a link to its field; it's empty otherwise. Screen readers announce it.

func (*Form) Errors

func (f *Form) Errors() []FieldError

Errors are the errors showing, in the fields' order.

func (*Form) Field

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

Field returns the field named name, or nil.

func (*Form) Input

func (f *Form) Input(name, label string, in *input.Input, rules ...Rule) *Field

Input adds a text field: it's marked required for screen readers, shows its error in its border and is checked when the user leaves it.

func (*Form) MarkClean

func (f *Form) MarkClean()

MarkClean takes the current values as the saved ones.

func (*Form) OnSubmit

func (f *Form) OnSubmit(fn func(values map[string]string)) *Form

OnSubmit sets what Submit does with the values once they're valid.

func (*Form) Reset

func (f *Form) Reset()

Reset puts back the saved values and clears the errors.

func (*Form) Row

func (f *Form) Row(name string, el elements.Element) elements.Element

Row lays out a field: its label (with a required marker), the element and its error under it.

func (*Form) Submit

func (f *Form) Submit()

Submit validates the form and, when it's valid, calls the OnSubmit handler.

func (*Form) Validate

func (f *Form) Validate() bool

Validate checks every field, shows their errors and focuses the first wrong one; it reports whether all are valid.

func (*Form) Value

func (f *Form) Value(name, label string, get func() string, set func(string), rules ...Rule) *Field

Value adds a field read by get and written by set (for a checkbox, a select, a date): any component. set may be nil when Reset needn't restore it.

func (*Form) Values

func (f *Form) Values() map[string]string

Values are the fields' values by name.

type Rule

type Rule func(value string) string

Rule checks a value and returns why it's wrong, or "".

func Check

func Check(fn func(string) error) Rule

Check makes a rule from a function returning an error.

func Email

func Email() Rule

Email needs an email address.

func Matches

func Matches(other func() string, msg string) Rule

Matches needs the same value as other, as a password confirmation does.

func MaxLength

func MaxLength(n int) Rule

MaxLength allows at most n characters.

func MinLength

func MinLength(n int) Rule

MinLength needs at least n characters.

func Number

func Number(lo, hi float64) Rule

Number needs a number between lo and hi.

func Pattern

func Pattern(re *regexp.Regexp, msg string) Rule

Pattern needs the value to match re; msg says what's expected.

func Required

func Required(msg ...string) Rule

Required needs a value other than spaces; msg replaces "Required".

Jump to

Keyboard shortcuts

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