form

package module
v0.0.29 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 2 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTagOptions added in v0.0.2

func GetTagOptions(fieldTag string) ([]fmt.KeyValue, bool)

GetTagOptions extracts options from a struct field tag using fmt.TagPairs.

func ParseOptionsTag added in v0.0.2

func ParseOptionsTag(tag string) []fmt.KeyValue

ParseOptionsTag parses "key1:text1,key2:text2" format into []fmt.KeyValue.

func RegisterInput added in v0.0.2

func RegisterInput(inputs ...input.Input)

RegisterInput registers input types for field mapping.

func SetGlobalClass added in v0.0.2

func SetGlobalClass(classes ...string)

Types

type Form

type Form struct {
	Inputs []input.Input
	// contains filtered or unexported fields
}

Form represents a form instance.

func New

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

New creates a new Form from a Fielder. parentID: ID of the parent DOM element where the form will be mounted. Returns an error if any exported field has no matching registered input.

func (*Form) GetID added in v0.0.17

func (f *Form) GetID() string

GetID returns the html id that group the form

func (*Form) Input added in v0.0.2

func (f *Form) Input(fieldName string) input.Input

Input returns the input with the given field name, or nil if not found.

func (*Form) OnSubmit added in v0.0.2

func (f *Form) OnSubmit(fn func(fmt.Fielder) error) *Form

OnSubmit sets the callback for form submission in WASM mode.

func (*Form) ParentID added in v0.0.2

func (f *Form) ParentID() string

ParentID returns the ID of the parent element.

func (*Form) RenderHTML added in v0.0.2

func (f *Form) RenderHTML() string

RenderHTML renders the form based on its SSR mode.

func (*Form) SetID added in v0.0.17

func (f *Form) SetID(id string)

SetID sets the html id that group the form

func (*Form) SetOptions added in v0.0.2

func (f *Form) SetOptions(fieldName string, opts ...fmt.KeyValue) *Form

SetOptions sets options for the input matching the given field name.

func (*Form) SetSSR added in v0.0.2

func (f *Form) SetSSR(enabled bool) *Form

SetSSR enables or disables SSR mode for this form.

func (*Form) SetValues added in v0.0.2

func (f *Form) SetValues(fieldName string, values ...string) *Form

SetValues sets values for the input matching the given field name.

func (*Form) SyncValues added in v0.0.2

func (f *Form) SyncValues(data fmt.Fielder) error

SyncValues copies all input values back into the bound struct via the Fielder's Pointers() method.

func (*Form) Validate added in v0.0.2

func (f *Form) Validate() error

Validate validates all inputs and returns the first error found.

func (*Form) ValidateData added in v0.0.26

func (f *Form) ValidateData(action byte, data fmt.Fielder) error

ValidateData validates a Fielder instance using this form's input rules. Satisfies the updated crudp.DataValidator interface (with fmt.Fielder).

type Namer added in v0.0.29

type Namer interface {
	FormName() string
}

Namer is optionally implemented by Fielder types to provide a custom name. If not implemented, the form derives the name from the first Schema field's context.

Directories

Path Synopsis
example
web command

Jump to

Keyboard shortcuts

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