See README.md for the consolidated API. This file contains additional detail.
f, err := form.New("content", data) // data implements model.Fielder
// -> f.GetID() == "content." + resolveStructName(data)
// -> f.String() renders all fields that have a Widget in data.Schema()For each field in data.Schema():
field.IsPK() && field.IsAutoInc()→ skip (auto-increment PKs not editable).field.Widget == nil→ skip (no UI binding).field.Widget.Clone(formID, fieldName).(input.Input)→ positioned input.field.NotNull→SetRequired(true)on the input.- Current value bound via
fmt.ReadValues()+SetValues().
- Skips fields with
SkipValidationset to true in the input. - Pulls values from reactive signals.
- Calls
inp.Validate(val)(promoted frommodel.Kind). - Returns the first error encountered.
Synchronizes input values back to the struct pointers provided by data.Pointers().
Supports model.FieldText, model.FieldInt, model.FieldFloat, and model.FieldBool.
Validates the provided data using the form's input rules. Satisfies crudp.DataValidator.
Runs the full submit pipeline programmatically:
SyncValues(f.data): copies values from signals to struct.Validate(): final validation check.- If valid and
OnSubmitis set:- Sets
submittingsignal to true. - Calls the
OnSubmitcallback. - When the callback's
donefunction is called:- Sets
submittingsignal back to false. - Resets the form (unless
NoResetOnSuccesswas called).
- Sets
- Sets
Returns the first validation error, or nil if the submission was dispatched.
The DOM submit event handler delegates to this method.
Optional capability interface for custom inputs that own their markup.
type Renderer interface {
RenderInput(value *dom.SignalString, onInput func(string)) *dom.Element
}- Markup ownership: The form still owns the field wrapper (
div.tw-field), the error span, and the field ID. The widget provides the inner control. - Contract: The widget must call
onInputwith the new value on user input. The form updates the value signal and runs live validation. - Location: Lives in package
formbecause it references*dom.Element(theinputpackage is dom-free).
type Permitted struct {
Letters bool // a-z, A-Z (and ñ/Ñ)
Tilde bool // á, é, í, ó, ú
Numbers bool // 0-9
Spaces bool // ' '
BreakLine bool // '\n'
Tab bool // '\t'
Extra []rune // additional allowed characters
NotAllowed []string // disallowed substrings
Minimum int // minimum length
Maximum int // maximum length
}Error messages from Permitted.Validate(name, text):
"{name} minimum {min} chars"— value shorter than Minimum"{name} maximum {max} chars"— value longer than Maximum"space not allowed"— space when Spaces=false"character {X} not allowed"— disallowed character
Fielder types can optionally implement Namer to provide a custom form name:
type Namer interface {
FormName() string
}If not implemented, defaults to "form".