form

package
v0.0.0-...-d1a9080 Latest Latest
Warning

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

Go to latest
Published: May 5, 2021 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IsRequired = validation.Required

IsRequired is an alias to validation.Required

View Source
var IsRequiredOrNull = validation.NilOrNotEmpty

IsRequiredOrNull is an alias to validation.NilOrNotEmpty

View Source
var IsValidEmail = is.Email

IsValidEmail adds an error to the field if it's not a valid email address

Functions

func Bind

func Bind(f *Form, r *http.Request, validators ...func(f *Form))

Bind loads and validates the data using the method tied to the request's content-type header.

func IsValidURL

func IsValidURL(schemes map[string]bool) validation.StringRule

IsValidURL checks is a url is a valid http or https url.

Types

type Errors

type Errors []error

Errors is a error list that is attached to a form or a field.

func (*Errors) Add

func (e *Errors) Add(err error)

Add adds an new error to the list.

func (Errors) MarshalJSON

func (e Errors) MarshalJSON() ([]byte, error)

MarshalJSON performs the error list's JSON serialization.

type Field

type Field struct {
	Name   string
	Type   reflect.Type
	Errors Errors
	// contains filtered or unexported fields
}

Field represents a form field.

func (*Field) MarshalJSON

func (f *Field) MarshalJSON() ([]byte, error)

MarshalJSON performs the field's JSON serialization.

func (*Field) Validate

func (f *Field) Validate(rules ...validation.Rule)

Validate applies the given validation rules to the field. When the validation fails, it adds an error to the field so it can be retrieved later.

func (*Field) Value

func (f *Field) Value() interface{}

Value returns the field's value.

type FieldValidator

type FieldValidator interface {
	Validate(*Field) error
}

FieldValidator is the interface for a field that implements its internal validation.

type Form

type Form struct {
	Fields map[string]*Field
	Errors Errors
	// contains filtered or unexported fields
}

Form contains the data instance and the derived fields that can be used in a template or returned in a JSON response.

func NewForm

func NewForm(instance interface{}) *Form

NewForm creates a new form instance and defines the derived fields.

func (*Form) BindJSON

func (f *Form) BindJSON(r io.Reader)

BindJSON decodes a JSON payload from an io.Reader into the form instance.

func (*Form) BindValues

func (f *Form) BindValues(values url.Values)

BindValues binds the values from any URL encoded value list.

func (*Form) Context

func (f *Form) Context() context.Context

Context returns the form current context

func (*Form) IsValid

func (f *Form) IsValid() bool

IsValid returns true when the form and its fields have no error

func (*Form) MarshalJSON

func (f *Form) MarshalJSON() ([]byte, error)

MarshalJSON performs the form's JSON serialization.

func (*Form) SetContext

func (f *Form) SetContext(ctx context.Context) *Form

SetContext set the new form's context

func (*Form) Validate

func (f *Form) Validate()

Validate performs the data validation on the form instance when it exists.

type Validator

type Validator interface {
	Validate(*Form)
}

Validator is the interface for types implementing a validation routine after the data had been converted.

Jump to

Keyboard shortcuts

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