go-form/content/docs/fields/_index.md
Simon Vieille 35c01fa7ca
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
add constraints
2025-07-22 21:34:33 +02:00

5.4 KiB

linkTitle title weight
Fields Fields 4

A field represents a field in a form.

Struct

type Field struct {
	Name        string
	Widget      string
	Data        any
	Options     []*Option
	Children    []*Field
	Constraints []validation.Constraint
	Errors      []validation.Error
	PrepareView func() map[string]any
	BeforeMount func(data any) (any, error)
	BeforeBind  func(data any) (any, error)
	Validate    func(f *Field) bool
	IsSlice     bool
	IsFixedName bool
	Form        *Form
	Parent      *Field
}

Fields

func NewField(name, widget string) *Field

Generates a new field with default properties It should not be used directly but inside function like in form.NewFieldText

Checkbox

func NewFieldCheckbox(name string) *Field

Generates an input[type=checkbox]

Choice

func NewFieldChoice(name string) *Field

Generates inputs (checkbox or radio) or selects

Csrf

func NewFieldCsrf(name string) *Field

Date

func NewFieldDate(name string) *Field

Generates an input[type=date] with default transformers

Datetime

func NewFieldDatetime(name string) *Field

Generates an input[type=datetime] with default transformers

DatetimeLocal

func NewFieldDatetimeLocal(name string) *Field

Generates an input[type=datetime-local] with default transformers

Hidden

func NewFieldHidden(name string) *Field

Generates an input[type=hidden]

Mail

func NewFieldMail(name string) *Field

Generates an input[type=email]

Number

func NewFieldNumber(name string) *Field

Generates an input[type=number] with default transformers

Password

func NewFieldPassword(name string) *Field

Generates an input[type=password]

Range

func NewFieldRange(name string) *Field

Generates an input[type=range]

Sub Form

func NewFieldSubForm(name string) *Field

Alias:

func NewSubForm(name string) *Field

Generates a sub form

Text

func NewFieldText(name string) *Field

Generates an input[type=text]

Textarea

func NewFieldTextarea(name string) *Field

Generates a textarea

Time

func NewFieldTime(name string) *Field

Generates an input[type=time] with default transformers

Submit

func NewSubmit(name string) *Field

Generates an input[type=submit]

Methods

Add

func (f *Field) Add(children ...*Field) *Field

Appends children

Bind

func (f *Field) Bind(data map[string]any, key *string) error

Bind the data into the given map

GetChild

func (f *Field) GetChild(name string) *Field

Returns a child using its name

GetId

func (f *Field) GetId() string

Computes the id of the field

GetName

func (f *Field) GetName() string

Computes the name of the field

GetOption

func (f *Field) GetOption(name string) *Option

Returns an option using its name

HasChild

func (f *Field) HasChild(name string) bool

Checks if the field contains a child using its name

HasOption

func (f *Field) HasOption(name string) bool

Checks if the field contains an option using its name

Mount

func (f *Field) Mount(data any) error

Populates the field with data

ResetErrors

func (f *Field) ResetErrors() *Field

Resets the field errors

WithBeforeBind

func (f *Field) WithBeforeBind(callback func(data any) (any, error)) *Field

Sets a transformer applied to the data of a field before defining it in a structure

WithBeforeMount

func (f *Field) WithBeforeMount(callback func(data any) (any, error)) *Field

Sets a transformer applied to the structure data before displaying it in a field

WithConstraints

func (f *Field) WithConstraints(constraints ...validation.Constraint) *Field

Appends constraints

WithData

func (f *Field) WithData(data any) *Field

Sets data the field

WithFixedName

func (f *Field) WithFixedName() *Field

Sets that the name of the field is not computed

WithOptions

func (f *Field) WithOptions(options ...*Option) *Field

Common options

Name type description Info
required bool Add required="true" Does not apply a constraint
attr map[string]string List of extra attributes of the field
row_attr map[string]string List of extra attributes of the field's top container
label string The label of the field Usually show before the field
label_attr map[string]string List of extra attributes of the label
help string Helper of the field

Appends options to the field

WithSlice

func (f *Field) WithSlice() *Field

Sets that the field represents a data slice