form

package
v0.1.0-alpha.6 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2020 License: Apache-2.0 Imports: 15 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 Error

type Error struct {
	// Code    FormErrorCode `json:"id,omitempty"`
	Message string `json:"message"`
}

swagger:model formError

type ErrorAdder

type ErrorAdder interface {
	// AddError adds an error to the form.
	AddError(err *Error, names ...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 disabled="{{.Disabled}}">
	Disabled bool `json:"disabled,omitempty"`

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

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

	// Errors contains all validation errors this particular field has caused.
	Errors []Error `json:"errors,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, prefix string) error
}

type Fields

type Fields []Field

Fields contains multiple fields

swagger:model formFields

type HTMLForm

type HTMLForm struct {
	sync.RWMutex

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

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

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

	// Errors contains all form errors. These will be duplicates of the individual field errors.
	Errors []Error `json:"errors,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) AddError

func (c *HTMLForm) AddError(err *Error, names ...string)

AddError 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()

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, prefix string) error

func (*HTMLForm) Unset

func (c *HTMLForm) Unset(name string)

Unset removes a field from the container.

func (*HTMLForm) Value

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

type Resetter

type Resetter interface {
	// Reset resets errors.
	Reset()
}

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