form

package module
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 3 Imported by: 0

README

form

Project Badges

Fast, minimalist form generation from Go structs. Optimized for TinyGo and WASM.

Features

  • Zero-Dependency Core: Lightweight and fast.
  • WASM Optimized: Centralized event delegation minimizes binary size and memory usage.
  • Dual Mode Rendering: Seamlessly toggle between client-side (WASM) and Server-Side Rendering (SSR).
  • Auto-Matching: Automatically pairs struct fields with appropriate inputs based on names, aliases, and Tags.
  • Type-Safe Binding: Syncs struct data to inputs and back.

Quick Start

type User struct {
    Name  string `placeholder:"Full Name"`
    Email string 
    Role  string `options:"admin:Admin,user:User"`
}

// 1. Create form from struct
f, _ := form.New("parent-id", &User{Name: "John"})

// 3. Render
html := f.RenderHTML()

Documentation

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 {
	Value  any
	Inputs []input.Input
	// contains filtered or unexported fields
}

Form represents a form instance.

func New

func New(parentID string, structPtr any) (*Form, error)

New creates a new Form from a struct pointer. 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(any) 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() error

SyncValues synchronizes all input values back to the source struct.

func (*Form) Validate added in v0.0.2

func (f *Form) Validate() error

Validate validates all inputs and returns the first error found.

Directories

Path Synopsis
example
web command

Jump to

Keyboard shortcuts

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