multiforms

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package multiforms processes returned HTML forms that may contain child forms.

Acknowledgement: the origin of this code is the book Let's Go! by Alex Edwards. This version adds support for child forms.

Index

Constants

This section is empty.

Variables

View Source
var EmailRX = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")

EmailRX is a regular expression for an email address, as recommended by W3C and Web Hypertext Application Technology Working Group.

View Source
var WebFiles embed.FS

WebFiles are the package's web resources (templates and static files)

Functions

This section is empty.

Types

type Child

type Child struct {
	Parent     *Form
	ChildIndex int
}

Child specifies a child form.

func (*Child) ChildError

func (c *Child) ChildError(field string) string

ChildError returns the error for a child field

func (*Child) ChildStyle

func (c *Child) ChildStyle() template.HTMLAttr

ChildStyle gets the display attribute for child item - hidden for a child form template.

func (*Child) ChildValid

func (c *Child) ChildValid(field string) string

ChildValid returns a CSS class to indicate if a field is valid, for Bootstrap.

type Form

type Form struct {
	url.Values
	CSRFToken   string
	Errors      formErrors
	ChildErrors childErrors
}

Form holds form data (in url.Values) and validation errors.

func New

func New(data url.Values, token string) *Form

New returns an initialised Form structure.

func (*Form) ChildBool

func (f *Form) ChildBool(field string, ix int) bool

ChildBool returns a checkbox value from child form. Unlike other fields, only checked fields are returned, and the value is the child index.

func (*Form) ChildFile

func (f *Form) ChildFile(field string, i int, ix int, validType func(string) bool) string

ChildFile returns a file name from child form.

func (*Form) ChildFloat added in v1.3.1

func (f *Form) ChildFloat(field string, i int, ix int, min float64, max float64, precision float64) (n float64, s string)

ChildFloat accepts a floating point number with a minimum and maximum values from a child form. The precision specifies the maximum precision accepted: 0 = any, 1 = whole number, 0.5 = half values, 0.1 = tenths, etc. It allows blank values, and returns both the number and a tidily formatted string.

func (*Form) ChildGet

func (f *Form) ChildGet(field string, i int) string

ChildGet is deprecated. Use ChildText.

func (*Form) ChildIndex

func (f *Form) ChildIndex(field string, i int) (int, error)

ChildIndex returns the index of a child form. Indexes are used to match the returned child items against the original data.

func (*Form) ChildMin

func (f *Form) ChildMin(field string, i int, ix int, min int) int

ChildMin returns a number with a minimum value from a child form.

func (*Form) ChildPositive

func (f *Form) ChildPositive(field string, i int, ix int) int

ChildPositive returns a positive number from a child form.

func (*Form) ChildRequired

func (f *Form) ChildRequired(field string, i int, ix int) string

ChildRequired is deprecated. Use ChildText.

func (*Form) ChildSelect

func (f *Form) ChildSelect(field string, i int, ix int, nOptions int) (int, error)

ChildSelect returns of the value of an HTML select. It assumes values are integers, 0 ... nOption-1

func (*Form) ChildText added in v0.11.0

func (f *Form) ChildText(field string, i int, ix int, min int, max int) string

ChildText returns trimmed text a child form, validating the value length. Set min=1 for a required value, max<=0 for no upper size.

func (*Form) ChildTrimmed

func (f *Form) ChildTrimmed(field string, i int) string

ChildTrimmed is deprecated. Use ChildText.

func (*Form) Float

func (f *Form) Float(s string, field string, min float64, max float64) float64

Check that field value is float within range

func (*Form) MatchesPattern

func (f *Form) MatchesPattern(field string, pattern *regexp.Regexp)

MatchesPattern checks that a field matches a regular expression.

func (*Form) MaxLength

func (f *Form) MaxLength(field string, d int)

MaxLength checks that a field does not exceed a maximum number of characters.

func (*Form) MinLength

func (f *Form) MinLength(field string, d int)

MinLength checks that a field contains a minimum number of characters.

func (*Form) NChildItems

func (f *Form) NChildItems() int

NChildItems returns the number of child items in the form.

func (*Form) PermittedValues

func (f *Form) PermittedValues(field string, opts ...string)

PermittedValues checks that a specific field in the form matches one of a set of specific permitted values.

func (*Form) Positive

func (f *Form) Positive(field string) int

Positive checks that a field value is integer and >=0

func (*Form) Required

func (f *Form) Required(fields ...string)

Required checks that specific fields in the form data are present and not blank.

func (*Form) Valid

func (f *Form) Valid() bool

Valid returns true the form data has no errors.

Jump to

Keyboard shortcuts

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