h

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package h provides utilties for rendering HTML forms.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldSet

type FieldSet struct {
	Name       string
	Label      string
	Fields     []FormField
	IsFoldable bool
}

FieldSet is a FormField that groups multiple FormFields together.

func (FieldSet) ReadState

func (fs FieldSet) ReadState(r *http.Request, s *FormState)

ReadState implements the FormField interface.

func (FieldSet) RenderField

func (fs FieldSet) RenderField(state FormState) template.HTML

RenderField implements the FormField interface.

type FieldState

type FieldState struct {
	Value        string          //only used by InputFieldSpec
	Selected     map[string]bool //only used by SelectFieldSpec
	IsUnfolded   bool            //only used by FieldSet
	ErrorMessage string
}

FieldState describes the state of an <input> field within type FormState.

type FormField

type FormField interface {
	ReadState(*http.Request, *FormState)
	RenderField(FormState) template.HTML
}

FormField is something that can appear in an HTML form.

type FormSpec

type FormSpec struct {
	PostTarget  string
	SubmitLabel string
	Fields      []FormField
}

FormSpec describes an HTML form that is submitted to a POST endpoint.

func (FormSpec) ReadState

func (f FormSpec) ReadState(r *http.Request, s *FormState)

ReadState reads and validates the field value from r.PostForm, and stores it in the given FormState.

func (FormSpec) Render

func (f FormSpec) Render(r *http.Request, s FormState) template.HTML

Render produces the HTML for this form.

type FormState

type FormState struct {
	Fields map[string]*FieldState
}

FormState describes the state of an HTML form.

func (FormState) IsValid

func (s FormState) IsValid() bool

IsValid returns false if any field has a validation error.

type InputFieldSpec

type InputFieldSpec struct {
	Name             string
	Label            string
	InputType        string
	AutoFocus        bool
	AutocompleteMode string
	Rules            []ValidationRule
}

InputFieldSpec describes a single <input> field within type FormSpec.

func (InputFieldSpec) ReadState

func (f InputFieldSpec) ReadState(r *http.Request, formState *FormState)

ReadState reads and validates the field value from r.PostForm, and stores it in the given FormState.

func (InputFieldSpec) RenderField

func (f InputFieldSpec) RenderField(state FormState) template.HTML

RenderField produces the HTML for this field.

type MultilineInputFieldSpec added in v1.1.0

type MultilineInputFieldSpec struct {
	Name  string
	Label string
	Rules []ValidationRule
}

MultilineInputFieldSpec describes a single <input> field within type FormSpec.

func (MultilineInputFieldSpec) ReadState added in v1.1.0

func (f MultilineInputFieldSpec) ReadState(r *http.Request, formState *FormState)

ReadState reads and validates the field value from r.PostForm, and stores it in the given FormState.

func (MultilineInputFieldSpec) RenderField added in v1.1.0

func (f MultilineInputFieldSpec) RenderField(state FormState) template.HTML

RenderField produces the HTML for this field.

type SelectFieldSpec

type SelectFieldSpec struct {
	Name     string
	Label    string
	Options  []SelectOptionSpec
	ReadOnly bool
}

SelectFieldSpec is a FormField where values can be selected from a given set. It's rendered as a series of checkboxes.

func (SelectFieldSpec) ReadState

func (f SelectFieldSpec) ReadState(r *http.Request, formState *FormState)

ReadState implements the FormField interface.

func (SelectFieldSpec) RenderField

func (f SelectFieldSpec) RenderField(state FormState) template.HTML

RenderField implements the FormField interface.

type SelectOptionSpec

type SelectOptionSpec struct {
	Value string
	Label string
}

SelectOptionSpec describes an option that can be selected in a SelectFieldSpec.

type Snippet

type Snippet struct {
	T *template.Template
}

Snippet provides a convenience API around html/template.Template.

func NewSnippet

func NewSnippet(input string) Snippet

NewSnippet parses html/template code into a Snippet.

func (Snippet) Render

func (s Snippet) Render(data interface{}) template.HTML

Render renders the snippet with the given data.

type StaticField

type StaticField struct {
	Label string
	Value template.HTML
}

StaticField is a FormField with a static value.

func (StaticField) ReadState

func (f StaticField) ReadState(*http.Request, *FormState)

ReadState implements the FormField interface.

func (StaticField) RenderField

func (f StaticField) RenderField(FormState) template.HTML

RenderField implements the FormField interface.

type ValidationRule

type ValidationRule func(string) error

ValidationRule returns an error message if the given field value is invalid.

Jump to

Keyboard shortcuts

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