forms

package
v0.0.0-...-8fa2440 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2016 License: MIT Imports: 16 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SelectValueFuncs = map[string]func(fi *FieldInfo, req *http.Request) []SelectValue{}

Obtains select values. The map is keyed by ValueSet. Return a list of values in the desired order.

Functions

func FormHTML

func FormHTML(s interface{}, req *http.Request, htmlFunc HTMLFunc) *pongo2.Value

Generates HTML (as a pongo2 safe value) for the given form. The given function is used for field layout.

func GridHTML

func GridHTML(fi *FieldInfo, value string, req *http.Request, buf *bytes.Buffer)

Field layout functions with additional surrounding divs for layout support.

func InputHTML

func InputHTML(fi *FieldInfo, value string, req *http.Request, buf *bytes.Buffer)

Field layout function for the field itself only, without any surrounding divs, labels, etc.

Types

type ErrorInfo

type ErrorInfo struct {
	// Field to which this error pertains. May be nil.
	FieldInfo *FieldInfo

	// Error message.
	Message string
}

Represents a specific error with a form submission.

func (*ErrorInfo) Error

func (ei *ErrorInfo) Error() string

type ErrorSink

type ErrorSink interface {
	error
	Add(errorInfo ErrorInfo)
	Num() int
}

Represents a target for generated form errors.

type Errors

type Errors []*ErrorInfo

Represents zero or more errors with a form submission.

Implements ErrorSink.

func (*Errors) Add

func (e *Errors) Add(errorInfo ErrorInfo)

func (Errors) Error

func (e Errors) Error() string

Returns a list of the errors as a string. Returns the empty string if there are no errors.

func (Errors) Num

func (e Errors) Num() int

Number of errors.

type FieldInfo

type FieldInfo struct {
	FName         string   // struct field name
	Name          string   // <input name="..." />. Defaults to ID or FName.
	Type          string   // <input type="..." /> (or "select" or "textarea").
	Required      bool     // required=""
	Pattern       string   // pattern="..." (enforced on server as well)
	MaxLength     int      // 0: no max length
	Size          int      // field size. cols for textarea
	VSize         int      // rows for textarea
	FormatMessage string   // validation error format message
	Classes       []string // HTML classes
	ID            string   // HTML ID
	Autofocus     bool     // <input autofocus="" />
	Placeholder   string   // <input placeholder="..." />
	Label         string   // Label text
	ValueSet      string   // <select> value set.
}

Information about a form field.

func GetFieldInfo

func GetFieldInfo(sf reflect.StructField) FieldInfo

Get the information for a struct field.

The format is as follows:

Field  SomeType    `form:"TYPE[,required][,autofocus][,.htmlClass][,#htmlID][,<int>][,<int>!]"`

The integer tag expresses a field size. The integer tag with "!" expresses a maximum length.

TYPE is the HTML field type. For non-<input/> types, use the tag name ("select", "textarea", etc.)

Many HTML classes may be specified. If the HTML ID is not specified, the struct field name is used as the field name and HTML ID. Otherwise, the HTML ID is used as the field name and HTML ID.

The following additional field tags are also supported:

fmsg:          Format message, shown on validation error.
pattern:       Regexp to enforce on client and server.
placeholder:   Placeholder string.
label:         Label string.
set:           <select> value set.

type HTMLFunc

type HTMLFunc func(fi *FieldInfo, value string, req *http.Request, buf *bytes.Buffer)

type SelectValue

type SelectValue struct {
	// The actual value of the option sent to the server.
	Value string

	// The label shown to the user.
	Title string

	// Optgroup. Values in the same optgroup must be contiguous.  Optgroups
	// cannot be nested. Values with a Group of "" are outside any optgroup.
	Group string

	// Should the option be disabled?
	Disabled bool
}

An option for a <select> field.

type State

type State struct {
	Errors Errors
	// contains filtered or unexported fields
}

Form state.

func FromReq

func FromReq(f interface{}, req *http.Request) *State

func (*State) CanSubmit

func (s *State) CanSubmit() bool

Returns true if the form is being submitted and has no errors.

func (*State) FromReq

func (s *State) FromReq(f interface{}, req *http.Request)

Load form from request. Pass pointer to structure to fill.

func (*State) MustShow

func (fstate *State) MustShow(tplName string, args map[string]interface{})

Jump to

Keyboard shortcuts

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