Form
Key bindings
Section titled “Key bindings”| Key | Action |
|---|---|
Tab | Focus next field |
Shift+Tab | Focus previous field |
Enter | Submit form (unless focused field is a TextArea) |
Ctrl+S | Submit form (always) |
Esc | Cancel (fires onCancel) |
Enter passes through to TextArea fields for newlines; use Ctrl+S to submit when a TextArea is focused.
Compatible field types
Section titled “Compatible field types”Any component implementing the FormField interface works with Form: TextField, TextArea, Checkbox, Select, MultiSelect, RadioGroup. All fire change events and expose FieldValue() / SetFieldValue().
Getting values
Section titled “Getting values”form.SetOnSubmit(func(values map[string]any) { name := values["name"].(string) active := values["active"].(bool)})Keys are the names passed when adding fields (AddField("name", textField)).
FormBuilder
Section titled “FormBuilder”For programmatic form construction, see FormBuilder — it provides a fluent AddTextField, AddSelect, etc. API that registers each field and builds the Form in one chain.
Usage
form := components.NewForm(). AddTextField("name", "Name", "Your name"). AddSelect("role", "Role", []string{"Admin", "Editor", "Viewer"}). AddCheckbox("active", "Active")form.SetOnSubmit(func(values map[string]any) { fmt.Println("name:", values["name"])})
fmt.Println(form != nil)Methods
| Method | Signature | Description |
|---|---|---|
AddCheckbox | func (f *Form) AddCheckbox(name, label string) *Form | AddCheckbox adds a checkbox to the form. |
AddField | func (f *Form) AddField(field FormField) *Form | AddField adds a field to the form. |
AddRadioGroup | func (f *Form) AddRadioGroup(name, label string, options []string) *Form | AddRadioGroup adds a radio group to the form. |
AddSelect | func (f *Form) AddSelect(name, label string, options []string) *Form | AddSelect adds a select field to the form. |
AddTextField | func (f *Form) AddTextField(name, label, placeholder string) *Form | AddTextField adds a text field to the form. |
Clear | func (f *Form) Clear() *Form | Clear resets all fields to their default values. |
FocusIndex | func (f *Form) FocusIndex(index int) *Form | FocusIndex focuses the field at the given index. |
GetCheckbox | func (f *Form) GetCheckbox(name string) (*Checkbox, bool) | GetCheckbox returns a Checkbox by name. Returns nil, false if not found or if the field is not a Checkbox. |
GetField | func (f *Form) GetField(name string) FormField | GetField returns a field by name. |
GetMultiSelect | func (f *Form) GetMultiSelect(name string) (*MultiSelect, bool) | GetMultiSelect returns a MultiSelect by name. Returns nil, false if not found or if the field is not a MultiSelect. |
GetRadioGroup | func (f *Form) GetRadioGroup(name string) (*RadioGroup, bool) | GetRadioGroup returns a RadioGroup by name. Returns nil, false if not found or if the field is not a RadioGroup. |
GetSelect | func (f *Form) GetSelect(name string) (*Select, bool) | GetSelect returns a Select by name. Returns nil, false if not found or if the field is not a Select. |
GetTextArea | func (f *Form) GetTextArea(name string) (*TextArea, bool) | GetTextArea returns a TextArea by name. Returns nil, false if not found or if the field is not a TextArea. |
GetTextField | func (f *Form) GetTextField(name string) (*TextField, bool) | GetTextField returns a TextField by name. Returns nil, false if not found or if the field is not a TextField. |
GetValues | func (f *Form) GetValues() map[string]any | GetValues returns all field values as a map. |
IsValid | func (f *Form) IsValid() bool | IsValid returns true if all fields pass validation. |
SetOnCancel | func (f *Form) SetOnCancel(fn func()) *Form | SetOnCancel sets the callback for form cancellation. |
SetOnSubmit | func (f *Form) SetOnSubmit(fn func(values map[string]any)) *Form | SetOnSubmit sets the callback for form submission. |
SetValues | func (f *Form) SetValues(values map[string]any) error | SetValues sets multiple field values from a map. Keys are field names, values must match the expected type for each field: - TextField, TextArea, Select, RadioGroup: string - Checkbox: bool - MultiSelect: []string or []int (for indices) Returns nil if all values were set successfully. Returns SetValuesError containing details of any failures. |
Validate | func (f *Form) Validate() error | Validate validates all fields and returns the first error. |
ValidateAll | func (f *Form) ValidateAll() ValidationResult | ValidateAll validates all fields and returns all errors. This is useful for showing multiple validation errors at once. |
Types
type Checkbox Checkbox is a boolean toggle component.
It implements ValueProvider[bool]. type FormField FormField is the interface for form fields.
Implementations expose their value through the polymorphic SetFieldValue /
FieldValue / ClearField trio so the Form container does not need to know
the concrete field type. type MultiSelect MultiSelect allows multiple option selection.
It implements MultiValueProvider[string]. type RadioGroup RadioGroup is a single-choice option group.
It implements IndexedValueProvider[string]. type Select Select is a dropdown selection component.
It implements IndexedValueProvider[string]. type TextArea TextArea is a multi-line text input.
It implements ValueProvider[string]. type TextField TextField is a single-line text input with validation.
It implements ValueProvider[string]. type ValidationResult struct ValidationResult contains all validation errors from a form. | Field | Type | Description |
|---|---|---|
Errors | []FieldError |
type FieldError struct FieldError represents a validation error for a specific field. | Field | Type | Description |
|---|---|---|
Field | string | |
Message | string |