forms

package
v0.0.0-...-f01c647 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 13 Imported by: 0

README

Fields

For each HTML form field supported, there is a corresponding field struct.

non-input fields (Field interface)

For non-input fields, the struct should support the Field interface. This is for e.g. paragraphs, headings, and other non-input fields.

input fields (InputField interface)

For input fields, the struct should implement the InputField interface. This is for e.g. text inputs, checkboxes, and other input fields that contain data.

The currently supported fields are

  • Checkbox
  • Date
  • Number
  • Select
  • Text
  • Textarea
  • ID
  • Hidden

Conversion

Each field type has a Codec that converts a string value to and from and underlying value type.

For example, the Number field has an IntCodec that converts a string to an int and vice versa. The fields use a default codec, but this can be overridden by setting the Codec field on the field struct.

This can be useful for custom fields, e.g. select fields where the string value should be converted to an iota constant. Another example is for Date fields, where the format of the date string can be changed.

Adding fields

To add a new field, you need to add a new struct that implements the Field or InputField interface.

The current file naming convention is as follows:

  • field_input_<type>.go for input fields
  • field_<type>.go for non-input fields

The struct name should be Field<Type> where <type> is the type of the field.

Tasks

  • Create the new struct in field_<type>.go for Input fields or field_input_<type>.go for InputField fields.
  • Create the FieldKind constant for the new field in field_kind.go
  • Register the new field kind in pkg/views/forms/field_kind.go#init()
  • Handle the new field kind in pkg/views/forms/form.gohtml
  • Add the new field to the list of supported fields in this file.
  • If necessary, define a new Codec for a field type if an existing codec is not suitable.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterFieldKind

func RegisterFieldKind(fieldKind FieldKind)

Types

type BoolCodec

type BoolCodec struct{}

BoolCodec is a codec for boolean values

func (*BoolCodec) Decode

func (c *BoolCodec) Decode(value string) (interface{}, error)

Decode implements Decoder.Decode

func (*BoolCodec) Encode

func (c *BoolCodec) Encode(value interface{}) (string, error)

Encode implements Encoder.Encode

type CheckboxInputField

type CheckboxInputField struct {
	// Name is the name of the field.
	Name string
	// DisplayName is the display name of the field.
	DisplayName string
	// Required is true if the field is required.
	Required bool
	// Value is the string value of the field.
	Value string
	// Help is the help text of the field.
	Help string
	// Errors are the errors of the field.
	Errors []string
	// Codec is the codec of the field.
	Codec Codec
}

CheckboxInputField represents a checkbox input field.

func (*CheckboxInputField) GetErrors

func (f *CheckboxInputField) GetErrors() []string

GetErrors implements FieldDefinition.GetErrors

func (*CheckboxInputField) GetKind

func (f *CheckboxInputField) GetKind() FieldKind

GetKind implements FieldDefinition.GetKind

func (*CheckboxInputField) GetName

func (f *CheckboxInputField) GetName() string

GetName implements FieldDefinition.GetName

func (*CheckboxInputField) GetStringValue

func (f *CheckboxInputField) GetStringValue() string

GetStringValue implements FieldDefinition.GetStringValue

func (*CheckboxInputField) GetValue

func (f *CheckboxInputField) GetValue() (interface{}, error)

GetValue implements InputField.GetValue

func (*CheckboxInputField) HasErrors

func (f *CheckboxInputField) HasErrors() bool

HasErrors implements FieldDefinition.HasErrors

func (*CheckboxInputField) SetErrors

func (f *CheckboxInputField) SetErrors(errors []string)

SetErrors implements FieldDefinition.SetErrors

func (*CheckboxInputField) SetStringValue

func (f *CheckboxInputField) SetStringValue(value string)

SetStringValue implements FieldDefinition.SetStringValue

func (*CheckboxInputField) SetValue

func (f *CheckboxInputField) SetValue(value interface{}) error

SetValue implements InputField.SetValue

type Codec

type Codec interface {
	Encoder
	Decoder
}

Codec represents a serializer/deserializer for a field value.

func NewBoolCodec

func NewBoolCodec() Codec

func NewCodec

func NewCodec(encoder Encoder, decoder Decoder) Codec

NewCodec returns a new codec with the given encoder and decoder

func NewIntCodec

func NewIntCodec() Codec

func NewStringCodec

func NewStringCodec() Codec

func NewStringListCodec

func NewStringListCodec() Codec

func NewTimeCodec

func NewTimeCodec(format string) Codec

type DateInputField

type DateInputField struct {
	// Name is the name of the field.
	Name string
	// DisplayName is the display name of the field.
	DisplayName string
	// Required is true if the field is required.
	Required bool
	// Value is the string value of the field.
	Value string
	// MinValue is the minimum allowed value of the field.
	MinValue string
	// MaxValue is the maximum allowed value of the field.
	MaxValue string
	// Help is the help text of the field.
	Help string
	// Errors are the errors of the field.
	Errors []string
	// Codec is the codec of the field.
	Codec Codec
}

DateInputField represents a date input field.

func (*DateInputField) GetErrors

func (f *DateInputField) GetErrors() []string

GetErrors implements FieldDefinition.GetErrors

func (*DateInputField) GetKind

func (f *DateInputField) GetKind() FieldKind

GetKind implements FieldDefinition.GetKind

func (*DateInputField) GetName

func (f *DateInputField) GetName() string

GetName implements FieldDefinition.GetName

func (*DateInputField) GetStringValue

func (f *DateInputField) GetStringValue() string

GetStringValue implements FieldDefinition.GetStringValue

func (*DateInputField) GetValue

func (f *DateInputField) GetValue() (interface{}, error)

GetValue implements InputField.GetValue

func (*DateInputField) HasErrors

func (f *DateInputField) HasErrors() bool

HasErrors implements FieldDefinition.HasErrors

func (*DateInputField) SetErrors

func (f *DateInputField) SetErrors(errors []string)

SetErrors implements FieldDefinition.SetErrors

func (*DateInputField) SetStringValue

func (f *DateInputField) SetStringValue(value string)

SetStringValue implements FieldDefinition.SetStringValue

func (*DateInputField) SetValue

func (f *DateInputField) SetValue(value interface{}) error

SetValue implements InputField.SetValue

type Decoder

type Decoder interface {
	// Decode decodes the value from a string
	Decode(value string) (interface{}, error)
}

Decoder is the interface for decoding values

type Encoder

type Encoder interface {
	// Encode encodes the value to a string
	Encode(value interface{}) (string, error)
}

Encoder is the interface for serializing values

type Field

type Field interface {
	// GetKind returns the kind of the field.
	GetKind() FieldKind
}

Field represents a form field.

type FieldKind

type FieldKind string

FieldKind represents the kind of field.

const (
	// FieldKindUnknown is the default value for a field kind.
	FieldKindUnknown FieldKind = "unknown"

	// FieldKindOptionalBooleanInput is the kind of field that represents an optional boolean input.
	FieldKindOptionalBooleanInput FieldKind = "optionalBoolean"
	// FieldKindCheckboxInput is the kind of field that represents a checkbox input.
	FieldKindCheckboxInput FieldKind = "checkbox"
	// FieldKindDateInput is the kind of field that represents a date input.
	FieldKindDateInput FieldKind = "date"
	// FieldKindHiddenInput is the kind of field that represents a hidden input.
	FieldKindHiddenInput FieldKind = "hidden"
	// FieldKindID is the kind of field that represents an ID.
	FieldKindID FieldKind = "id"
	// FieldKindNumberInput is the kind of field that represents a number input.
	FieldKindNumberInput FieldKind = "number"
	// FieldKindSelect is the kind of field that represents a select input.
	FieldKindSelect FieldKind = "select"
	// FieldKindTextInput is the kind of field that represents a text input.
	FieldKindTextInput FieldKind = "text"
	// FieldKindTextarea is the kind of field that represents a textarea input.
	FieldKindTextarea FieldKind = "textarea"
)

func AllFieldKinds

func AllFieldKinds() []FieldKind

func KnownFieldKinds

func KnownFieldKinds() []FieldKind

type Form

type Form struct {
	Sections []*FormSection
	Title    string
}

func (*Form) HTML

func (f *Form) HTML() (template.HTML, error)

func (*Form) Into

func (f *Form) Into(i interface{}) error

func (*Form) ParseURLValues

func (f *Form) ParseURLValues(v url.Values)

func (*Form) SetErrors

func (f *Form) SetErrors(errors validation.ErrorList)

type FormSection

type FormSection struct {
	Title       string
	Collapsible bool
	Collapsed   bool
	Fields      []Field
}

type HiddenInputField

type HiddenInputField struct {
	// Name is the name of the field.
	Name string
	// Value is the string value of the field.
	Value string
	// Codec is the codec of the field.
	Codec Codec
}

HiddenInputField represents a hidden input field.

func (*HiddenInputField) GetErrors

func (f *HiddenInputField) GetErrors() []string

GetErrors implements FieldDefinition.GetErrors

func (*HiddenInputField) GetKind

func (f *HiddenInputField) GetKind() FieldKind

GetKind implements FieldDefinition.GetKind

func (*HiddenInputField) GetName

func (f *HiddenInputField) GetName() string

GetName implements FieldDefinition.GetName

func (*HiddenInputField) GetStringValue

func (f *HiddenInputField) GetStringValue() string

GetStringValue implements FieldDefinition.GetStringValue

func (*HiddenInputField) GetValue

func (f *HiddenInputField) GetValue() (interface{}, error)

GetValue implements InputField.GetValue

func (*HiddenInputField) HasErrors

func (f *HiddenInputField) HasErrors() bool

HasErrors implements FieldDefinition.HasErrors

func (*HiddenInputField) SetErrors

func (f *HiddenInputField) SetErrors(_ []string)

SetErrors implements FieldDefinition.SetErrors

func (*HiddenInputField) SetStringValue

func (f *HiddenInputField) SetStringValue(value string)

SetStringValue implements FieldDefinition.SetStringValue

func (*HiddenInputField) SetValue

func (f *HiddenInputField) SetValue(value interface{}) error

SetValue implements InputField.SetValue

type IDField

type IDField struct {
	// QRCodeURL is the URL of the QR code.
	QRCodeURL string
	// Name is the name of the field.
	Name string
	// DisplayName is the display name of the field.
	DisplayName string
	// Required is true if the field is required.
	Required bool
	// Value is the string value of the field.
	Value string
	// Help is the help text of the field.
	Help string
	// Errors are the errors of the field.
	Errors []string
	// Codec is the codec of the field.
	Codec Codec
}

IDField represents an ID field.

func (*IDField) GetErrors

func (f *IDField) GetErrors() []string

GetErrors implements FieldDefinition.GetErrors

func (*IDField) GetKind

func (f *IDField) GetKind() FieldKind

GetKind implements FieldDefinition.GetKind

func (*IDField) GetName

func (f *IDField) GetName() string

GetName implements FieldDefinition.GetName

func (*IDField) GetStringValue

func (f *IDField) GetStringValue() string

GetStringValue implements FieldDefinition.GetStringValue

func (*IDField) GetValue

func (f *IDField) GetValue() (interface{}, error)

GetValue implements InputField.GetValue

func (*IDField) HasErrors

func (f *IDField) HasErrors() bool

HasErrors implements FieldDefinition.HasErrors

func (*IDField) SetErrors

func (f *IDField) SetErrors(errors []string)

SetErrors implements FieldDefinition.SetErrors

func (*IDField) SetStringValue

func (f *IDField) SetStringValue(value string)

SetStringValue implements FieldDefinition.SetStringValue

func (*IDField) SetValue

func (f *IDField) SetValue(value interface{}) error

SetValue implements InputField.SetValue

type InputField

type InputField interface {
	Field
	// GetName returns the name of the field.
	GetName() string
	// GetStringValue returns the value of the field.
	GetStringValue() string
	// SetStringValue sets the value of the field.
	SetStringValue(value string)
	// GetValue returns the value of the field.
	GetValue() (interface{}, error)
	// SetValue sets the value of the field.
	SetValue(value interface{}) error
	// SetErrors sets the errors of the field.
	SetErrors(errors []string)
	// HasErrors returns true if the field has errors.
	HasErrors() bool
	// GetErrors returns the errors of the field.
	GetErrors() []string
}

InputField represents an input field. It is a field that can be used to input data.

type IntCodec

type IntCodec struct{}

IntCodec is a codec for integer values

func (*IntCodec) Decode

func (c *IntCodec) Decode(value string) (interface{}, error)

Decode implements Decoder.Decode

func (*IntCodec) Encode

func (c *IntCodec) Encode(value interface{}) (string, error)

Encode implements Encoder.Encode

type NumberInputField

type NumberInputField struct {
	// Name is the name of the field.
	Name string
	// DisplayName is the display name of the field.
	DisplayName string
	// Required is true if the field is required.
	Required bool
	// Value is the string value of the field.
	Value string
	// Help is the help text of the field.
	Help string
	// Errors are the errors of the field.
	Errors []string
	// Codec is the codec of the field.
	Codec Codec
}

NumberInputField represents a number input field.

func (*NumberInputField) GetErrors

func (f *NumberInputField) GetErrors() []string

GetErrors implements FieldDefinition.GetErrors

func (*NumberInputField) GetKind

func (f *NumberInputField) GetKind() FieldKind

GetKind implements FieldDefinition.GetKind

func (*NumberInputField) GetName

func (f *NumberInputField) GetName() string

GetName implements FieldDefinition.GetName

func (*NumberInputField) GetStringValue

func (f *NumberInputField) GetStringValue() string

GetStringValue implements FieldDefinition.GetStringValue

func (*NumberInputField) GetValue

func (f *NumberInputField) GetValue() (interface{}, error)

GetValue implements InputField.GetValue

func (*NumberInputField) HasErrors

func (f *NumberInputField) HasErrors() bool

HasErrors implements FieldDefinition.HasErrors

func (*NumberInputField) SetErrors

func (f *NumberInputField) SetErrors(errors []string)

SetErrors implements FieldDefinition.SetErrors

func (*NumberInputField) SetStringValue

func (f *NumberInputField) SetStringValue(value string)

SetStringValue implements FieldDefinition.SetStringValue

func (*NumberInputField) SetValue

func (f *NumberInputField) SetValue(value interface{}) error

SetValue implements InputField.SetValue

type OptionalBooleanInputField

type OptionalBooleanInputField struct {
	// Name is the name of the field.
	Name string
	// DisplayName is the display name of the field.
	DisplayName string
	// Required is true if the field is required.
	Required bool
	// Value is the string value of the field.
	Value string
	// Help is the help text of the field.
	Help string
	// Errors are the errors of the field.
	Errors []string
	// Codec is the codec of the field.
	Codec Codec
}

OptionalBooleanInputField represents a checkbox input field.

func (*OptionalBooleanInputField) GetErrors

func (f *OptionalBooleanInputField) GetErrors() []string

GetErrors implements FieldDefinition.GetErrors

func (*OptionalBooleanInputField) GetKind

func (f *OptionalBooleanInputField) GetKind() FieldKind

GetKind implements FieldDefinition.GetKind

func (*OptionalBooleanInputField) GetName

func (f *OptionalBooleanInputField) GetName() string

GetName implements FieldDefinition.GetName

func (*OptionalBooleanInputField) GetStringValue

func (f *OptionalBooleanInputField) GetStringValue() string

GetStringValue implements FieldDefinition.GetStringValue

func (*OptionalBooleanInputField) GetValue

func (f *OptionalBooleanInputField) GetValue() (interface{}, error)

GetValue implements InputField.GetValue

func (*OptionalBooleanInputField) HasErrors

func (f *OptionalBooleanInputField) HasErrors() bool

HasErrors implements FieldDefinition.HasErrors

func (*OptionalBooleanInputField) IsSelected

func (f *OptionalBooleanInputField) IsSelected(value string) bool

func (*OptionalBooleanInputField) SetErrors

func (f *OptionalBooleanInputField) SetErrors(errors []string)

SetErrors implements FieldDefinition.SetErrors

func (*OptionalBooleanInputField) SetStringValue

func (f *OptionalBooleanInputField) SetStringValue(value string)

SetStringValue implements FieldDefinition.SetStringValue

func (*OptionalBooleanInputField) SetValue

func (f *OptionalBooleanInputField) SetValue(value interface{}) error

SetValue implements InputField.SetValue

type SelectInputField

type SelectInputField struct {
	// Name is the name of the field.
	Name string
	// DisplayName is the display name of the field.
	DisplayName string
	// Required is true if the field is required.
	Required bool
	// Value is the string value of the field.
	Value string
	// Help is the help text of the field.
	Help string
	// Options are the options of the field.
	Errors []string
	// AllowMultiple is true if the field allows multiple values.
	AllowMultiple bool
	// Options are the options of the field.
	Options []SelectInputFieldOption
	// Codec is the codec of the field.
	Codec Codec
}

SelectInputField represents a select input field.

func (*SelectInputField) GetErrors

func (f *SelectInputField) GetErrors() []string

GetErrors implements FieldDefinition.GetErrors

func (*SelectInputField) GetKind

func (f *SelectInputField) GetKind() FieldKind

GetKind implements FieldDefinition.GetKind

func (*SelectInputField) GetName

func (f *SelectInputField) GetName() string

GetName implements FieldDefinition.GetName

func (*SelectInputField) GetStringValue

func (f *SelectInputField) GetStringValue() string

GetStringValue implements FieldDefinition.GetStringValue

func (*SelectInputField) GetValue

func (f *SelectInputField) GetValue() (interface{}, error)

GetValue implements InputField.GetValue

func (*SelectInputField) HasErrors

func (f *SelectInputField) HasErrors() bool

HasErrors implements FieldDefinition.HasErrors

func (*SelectInputField) IsSelected

func (f *SelectInputField) IsSelected(value string) bool

func (*SelectInputField) SetErrors

func (f *SelectInputField) SetErrors(errors []string)

SetErrors implements FieldDefinition.SetErrors

func (*SelectInputField) SetStringValue

func (f *SelectInputField) SetStringValue(value string)

SetStringValue implements FieldDefinition.SetStringValue

func (*SelectInputField) SetValue

func (f *SelectInputField) SetValue(value interface{}) error

SetValue implements InputField.SetValue

type SelectInputFieldOption

type SelectInputFieldOption struct {
	// Value is the value of the option.
	Value string
	// Label is the label of the option.
	Label string
}

SelectInputFieldOption represents an option of a select input field.

type StringCodec

type StringCodec struct{}

StringCodec is a codec for string values

func (*StringCodec) Decode

func (c *StringCodec) Decode(value string) (interface{}, error)

Decode implements Decoder.Decode

func (*StringCodec) Encode

func (c *StringCodec) Encode(value interface{}) (string, error)

Encode implements Encoder.Encode

type StringListCodec

type StringListCodec struct{}

StringListCodec is a codec for []string values

func (*StringListCodec) Decode

func (c *StringListCodec) Decode(value string) (interface{}, error)

Decode implements Decoder.Decode

func (*StringListCodec) Encode

func (c *StringListCodec) Encode(value interface{}) (string, error)

Encode implements Encoder.Encode

type TextAreaInputField

type TextAreaInputField struct {
	// Name is the name of the field.
	Name string
	// DisplayName is the display name of the field.
	DisplayName string
	// Required is true if the field is required.
	Required bool
	// Value is the string value of the field.
	Value string
	// Help is the help text of the field.
	Help string
	// Errors are the errors of the field.
	Errors []string
	// Codec is the codec of the field.
	Codec Codec

	Rows int
}

TextAreaInputField represents a text area input field.

func (*TextAreaInputField) GetErrors

func (f *TextAreaInputField) GetErrors() []string

GetErrors implements FieldDefinition.GetErrors

func (*TextAreaInputField) GetKind

func (f *TextAreaInputField) GetKind() FieldKind

GetKind implements FieldDefinition.GetKind

func (*TextAreaInputField) GetName

func (f *TextAreaInputField) GetName() string

GetName implements FieldDefinition.GetName

func (*TextAreaInputField) GetStringValue

func (f *TextAreaInputField) GetStringValue() string

GetStringValue implements FieldDefinition.GetStringValue

func (*TextAreaInputField) GetValue

func (f *TextAreaInputField) GetValue() (interface{}, error)

GetValue implements InputField.GetValue

func (*TextAreaInputField) HasErrors

func (f *TextAreaInputField) HasErrors() bool

HasErrors implements FieldDefinition.HasErrors

func (*TextAreaInputField) SetErrors

func (f *TextAreaInputField) SetErrors(errors []string)

SetErrors implements FieldDefinition.SetErrors

func (*TextAreaInputField) SetStringValue

func (f *TextAreaInputField) SetStringValue(value string)

SetStringValue implements FieldDefinition.SetStringValue

func (*TextAreaInputField) SetValue

func (f *TextAreaInputField) SetValue(value interface{}) error

SetValue implements InputField.SetValue

type TextInputField

type TextInputField struct {
	// Name is the name of the field.
	Name string
	// DisplayName is the display name of the field.
	DisplayName string
	// Required is true if the field is required.
	Required bool
	// Value is the string value of the field.
	Value string
	// Help is the help text of the field.
	Help string
	// Errors are the errors of the field.
	Errors []string
	// Codec
	Codec Codec
}

TextInputField represents a text input field.

func (*TextInputField) GetErrors

func (f *TextInputField) GetErrors() []string

GetErrors implements FieldDefinition.GetErrors

func (*TextInputField) GetKind

func (f *TextInputField) GetKind() FieldKind

GetKind implements FieldDefinition.GetKind

func (*TextInputField) GetName

func (f *TextInputField) GetName() string

GetName implements FieldDefinition.GetName

func (*TextInputField) GetStringValue

func (f *TextInputField) GetStringValue() string

GetStringValue implements FieldDefinition.GetStringValue

func (*TextInputField) GetValue

func (f *TextInputField) GetValue() (interface{}, error)

GetValue implements InputField.GetValue

func (*TextInputField) HasErrors

func (f *TextInputField) HasErrors() bool

HasErrors implements FieldDefinition.HasErrors

func (*TextInputField) SetErrors

func (f *TextInputField) SetErrors(errors []string)

SetErrors implements FieldDefinition.SetErrors

func (*TextInputField) SetStringValue

func (f *TextInputField) SetStringValue(value string)

SetStringValue implements FieldDefinition.SetStringValue

func (*TextInputField) SetValue

func (f *TextInputField) SetValue(value interface{}) error

SetValue implements InputField.SetValue

type TimeCodec

type TimeCodec struct {
	// contains filtered or unexported fields
}

TimeCodec is a codec for time.Time values

func (*TimeCodec) Decode

func (c *TimeCodec) Decode(value string) (interface{}, error)

Decode implements Decoder.Decode

func (*TimeCodec) Encode

func (c *TimeCodec) Encode(value interface{}) (string, error)

Encode implements Encoder.Encode

Jump to

Keyboard shortcuts

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