form

package
v0.4.5-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2020 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const CSRFTokenName = "csrf_token"
View Source
const DisableFormField = "disableFormField"

Variables

This section is empty.

Functions

func CSRFFormFieldGenerator

func CSRFFormFieldGenerator(g CSRFGenerator) func(r *http.Request) *Field

Types

type CSRFGenerator

type CSRFGenerator func(r *http.Request) string

type CSRFSetter

type CSRFSetter interface {
	// SetCSRF sets the CSRF value for the form.
	SetCSRF(string)
}

type ErrorParser

type ErrorParser interface {
	// ParseError type asserts the given error and sets the forms's errors or a
	// field's errors and if the error is not something to be handled by the
	// form itself, the error is returned for further propagation (e.g. showing a 502 status code).
	ParseError(err error) error
}

ErrorParser is capable of parsing and processing errors.

type Field

type Field struct {
	// Name is the equivalent of `<input name="{{.Name}}">`
	//
	// required: true
	Name string `json:"name"`

	// Type is the equivalent of `<input type="{{.Type}}">`
	//
	// required: true
	Type string `json:"type"`

	// Pattern is the equivalent of `<input pattern="{{.Pattern}}">`
	Pattern string `json:"pattern,omitempty"`

	// Disabled is the equivalent of `<input {{if .Disabled}}disabled{{end}}">`
	Disabled bool `json:"disabled,omitempty"`

	// Required is the equivalent of `<input required="{{.Required}}">`
	Required bool `json:"required,omitempty"`

	// Value is the equivalent of `<input value="{{.Value}}">`
	Value interface{} `json:"value,omitempty" faker:"string"`

	// Messages contains a list of messages (e.g. validation errors) that affect this field.
	Messages text.Messages `json:"messages,omitempty"`
}

Field represents a HTML Form Field

swagger:model formField

func (*Field) Reset

func (f *Field) Reset()

Reset resets a field's value and errors.

type FieldSetter

type FieldSetter interface {
	// SetField sets a field of the form.
	SetField(field Field)
}

type FieldSorter

type FieldSorter interface {
	SortFields(schemaRef string) error
}

type FieldUnsetter

type FieldUnsetter interface {
	// UnsetFields removes a field from the form.
	UnsetField(name string)
}

type Fields

type Fields []Field

Fields contains multiple fields

swagger:model formFields

type HTMLForm

type HTMLForm struct {
	sync.RWMutex `faker:"-"`

	// Action should be used as the form action URL `<form action="{{ .Action }}" method="post">`.
	//
	// required: true
	Action string `json:"action" faker:"url"`

	// Method is the form method (e.g. POST)
	//
	// required: true
	Method string `json:"method" faker:"http_method"`

	// Fields contains the form fields.
	//
	// required: true
	Fields Fields `json:"fields"`

	// Messages contains all global form messages and errors.
	Messages text.Messages `json:"messages,omitempty"`
}

HTMLForm represents a HTML Form. The container can work with both HTTP Form and JSON requests

swagger:model form

func NewHTMLForm

func NewHTMLForm(action string) *HTMLForm

NewHTMLForm returns an empty container.

func NewHTMLFormFromJSON

func NewHTMLFormFromJSON(action string, raw json.RawMessage, prefix string) *HTMLForm

NewHTMLFormFromJSON creates a HTML form based on the provided JSON struct.

func NewHTMLFormFromJSONSchema

func NewHTMLFormFromJSONSchema(action, jsonSchemaRef, prefix string, compiler *jsonschema.Compiler) (*HTMLForm, error)

NewHTMLFormFromJSONSchema creates a new HTMLForm and populates the fields using the provided JSON Schema.

func NewHTMLFormFromRequestBody

func NewHTMLFormFromRequestBody(r *http.Request, action string, compiler decoderx.HTTPDecoderOption) (*HTMLForm, error)

NewHTMLFormFromRequestBody creates a new HTMLForm and populates fields by parsing the HTTP Request body. A jsonSchemaRef needs to be added to allow HTTP Form Post Body parsing.

func (*HTMLForm) AddMessage

func (c *HTMLForm) AddMessage(err *text.Message, setForFields ...string)

AddMessage adds the provided error, and if a non-empty names list is set, adds the error on the corresponding field.

func (*HTMLForm) ParseError

func (c *HTMLForm) ParseError(err error) error

ParseError type asserts the given error and sets the container's errors or a field's errors and if the error is not something to be handled by the form container, the error is returned.

This method DOES NOT touch the values of the form fields, only its errors.

func (*HTMLForm) Reset

func (c *HTMLForm) Reset(exclude ...string)

Reset resets the container's errors as well as each field's value and errors.

func (*HTMLForm) ResetMessages

func (c *HTMLForm) ResetMessages(exclude ...string)

Reset resets the container's errors as well as each field's value and errors.

func (*HTMLForm) Scan

func (c *HTMLForm) Scan(value interface{}) error

func (*HTMLForm) SetCSRF

func (c *HTMLForm) SetCSRF(token string)

SetCSRF sets the CSRF value using e.g. nosurf.Token(r).

func (*HTMLForm) SetField

func (c *HTMLForm) SetField(field Field)

SetField sets a field.

func (*HTMLForm) SetRequired

func (c *HTMLForm) SetRequired(fields ...string)

SetRequired sets the container's fields required.

func (*HTMLForm) SetValue

func (c *HTMLForm) SetValue(name string, value interface{})

SetValue sets a container's field to the provided name and value.

func (*HTMLForm) SetValues

func (c *HTMLForm) SetValues(values map[string]interface{})

SetValues sets the container's fields to the provided values.

func (*HTMLForm) SetValuesFromJSON

func (c *HTMLForm) SetValuesFromJSON(raw json.RawMessage, prefix string)

SetValuesFromJSON sets the container's fields to the provided values.

func (*HTMLForm) SortFields

func (c *HTMLForm) SortFields(schemaRef string) error

func (*HTMLForm) UnsetField

func (c *HTMLForm) UnsetField(name string)

Unset removes a field from the container.

func (*HTMLForm) Value

func (c *HTMLForm) Value() (driver.Value, error)

type MessageAdder

type MessageAdder interface {
	// AddMessage adds a message to the form. A message can also be set for one or more fields if
	// `setForFields` is set.
	AddMessage(err *text.Message, setForFields ...string)
}

type MessageResetter

type MessageResetter interface {
	// ResetMessages resets the form's or field's messages..
	ResetMessages(exclude ...string)
}

type Resetter

type Resetter interface {
	// Resets the form or field.
	Reset(exclude ...string)
}

type ValueSetter

type ValueSetter interface {
	// SetValue sets a value of the form.
	SetValue(name string, value interface{})
}

Jump to

Keyboard shortcuts

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