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 ¶
- func Label(text string, required bool) elements.Element
- type Field
- type FieldError
- type Form
- func (f *Form) Dirty() bool
- func (f *Form) DirtyFields() []string
- func (f *Form) ErrorSummary() elements.Element
- func (f *Form) Errors() []FieldError
- func (f *Form) Field(name string) *Field
- func (f *Form) Input(name, label string, in *input.Input, rules ...Rule) *Field
- func (f *Form) MarkClean()
- func (f *Form) OnSubmit(fn func(values map[string]string)) *Form
- func (f *Form) Reset()
- func (f *Form) Row(name string, el elements.Element) elements.Element
- func (f *Form) Submit()
- func (f *Form) Validate() bool
- func (f *Form) Value(name, label string, get func() string, set func(string), rules ...Rule) *Field
- func (f *Form) Values() map[string]string
- type Rule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Field ¶
type Field struct {
Name, Label string
// contains filtered or unexported fields
}
Field is one value of a form.
func (*Field) Error ¶
Error is the field's error, once shown: after the user left the field, or tried to submit.
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 (*Form) Dirty ¶
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 ¶
DirtyFields are the names of the changed fields.
func (*Form) ErrorSummary ¶
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) Input ¶
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) Row ¶
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 ¶
Validate checks every field, shows their errors and focuses the first wrong one; it reports whether all are valid.