forms

package
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: GPL-2.0 Imports: 9 Imported by: 0

Documentation

Rendered for js/wasm

Index

Constants

View Source
const Delimiter = "___"

Form delimiter for nested structs

Variables

This section is empty.

Functions

func FormDataToStruct

func FormDataToStruct(data map[string]string, s any) error

Parse form data to a struct.

func FormatIfDateTime

func FormatIfDateTime(val any) any

Format a value for display in a form

func SetValue

func SetValue(s any, column string, value any)

Set a value on a struct

func SetValueStrict

func SetValueStrict(s any, f reflect.StructField, v reflect.Value, column string, value any)

Set a value on a struct, faster option.

func TransformValue

func TransformValue(s any, field string, val any) (any, error)

Transform a form value to the correct type

Types

type FORMTYPES

type FORMTYPES string

Valid formtypes

const (
	FORMTYP_TEXT     FORMTYPES = "text"
	FORMTYP_CHECKBOX FORMTYPES = "checkbox"
	FORMTYP_NUMBER   FORMTYPES = "number"
	FORMTYP_DATETIME FORMTYPES = "datetime-local"
	FORMTYP_FILE     FORMTYPES = "file"
	FORMTYP_STRUCT   FORMTYPES = "struct"
	// FORMTYP_LIST     FORMTYPES = "select"
	FORMTYP_INVALID FORMTYPES = "text"
)

Formtypes to use in forms

func ReflectInputType

func ReflectInputType(val any) FORMTYPES

Reflect the form input type of a value

func (FORMTYPES) Equals

func (ft FORMTYPES) Equals(other FORMTYPES) bool

Check if two formtypes are equal

type Form

type Form struct {
	Inner      *elements.Element
	Validators map[string]func(string) error
}

Form struct

func NewForm

func NewForm(action string, method string) *Form

Get a new form

func StructToForm

func StructToForm(s any, labelClass, inputClass, action, method string) *Form

Render a struct into a form

func (*Form) AddValidator

func (f *Form) AddValidator(name string, fn func(string) error)

Add a validator to the form. The validator will be called when the form is submitted. WATCH OUT!

  • The form names could be transformed when you parse a struct into a form; Please use the same name as the struct field name, for embedded structs the delimiter is used:

    "Field" for a normal field "Other___Field" for an embedded field

func (*Form) AttrID

func (f *Form) AttrID(id string) *Form

Set the form ID

func (*Form) Element

func (f *Form) Element() *elements.Element

Get the form value from the form element

func (*Form) OnSubmit

func (f *Form) OnSubmit(cb func(data map[string]string, elements []jsext.Element))

Eventlistener for when the form is submitted.

func (*Form) OnSubmitToStruct

func (f *Form) OnSubmitToStruct(strct any, fn func(strct any, elements []jsext.Element))

Eventlistener for when the form is submitted, data is parsed into a struct

func (*Form) Render

func (f *Form) Render() jsext.Element

Render the form

func (*Form) Value

func (f *Form) Value() jsext.Element

Get the form value from the form element

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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