Skip to content

tinywasm/form

Repository files navigation

tinywasm/form

Project Badges

Fast form generation from Go structs for TinyGo + WASM. Uses fmt.Fielder interface for zero-reflection data binding.

Quick Start

// User struct implements fmt.Fielder (usually generated by ormc)
f, err := form.New("parent-id", &User{Name: "John"})
html := f.RenderHTML()    // render to HTML string

Public API

form.New(parentID string, data fmt.Fielder) (*Form, error)

Creates a Form from a Fielder.

  • Uses data.Schema() to build inputs.
  • Matches each field to a registered Input by name/alias or Field.Input.
  • Copies data.Values() into inputs.
  • Form id = parentID + "." + data.FormName().

form.RegisterInput(inputs ...input.Input)

Registers custom input types globally.

(*Form) Methods

Method Description
GetID() string Returns form's HTML id
SetSSR(bool) *Form Enables SSR mode
RenderHTML() string Generates form HTML
Validate() error Validates all inputs
SyncValues(fmt.Fielder) error Copies input values back into the provided data
ValidateData(byte, fmt.Fielder) error Validates data (crudp.DataValidator)
OnSubmit(func(fmt.Fielder) error) *Form Sets WASM submit callback
OnMount() WASM only — sets up event delegation
Input(fieldName string) input.Input Returns input for a field name

Migration Guide

If you were using form.New(parentID, &MyStruct{}), you must now ensure MyStruct implements fmt.Fielder. This is typically handled by tinywasm/orm's ormc tool.

API changes:

  • SyncValues()SyncValues(data)
  • OnSubmit(func(any) error)OnSubmit(func(fmt.Fielder) error)
  • ValidateData(action, ...any)ValidateData(action, fmt.Fielder)

Struct Tags

Tags are now parsed at generation time by ormc.

Tag Format Description
form "email" Force input type
form "-" Exclude from form
options "k:v" Select options
validate "false" Skip validation

Documentation Index

About

Form generation using DOM and structs

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages