forms

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: BSD-1-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeText     = "text"
	TypePassword = "password"
	TypeEmail    = "email"
	TypeNumber   = "number"
	TypeRange    = "range"
	TypeTextArea = "textarea"
	TypeCheck    = "checkbox"
	TypeRadio    = "radio"
	TypeSelect   = "select"
	TypeHidden   = "hidden"
	TypeFile     = "file"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element string

func (Element) HTML

func (e Element) HTML() template.HTML

func (Element) String

func (e Element) String() string

type ElementInterface added in v1.0.6

type ElementInterface interface {
	String() string
	HTML() template.HTML
}

type Field

type Field struct {
	LabelText    string
	LabelClass   string
	ID           string
	Class        string
	Placeholder  string
	Type         string
	Name         string
	FormValue    *FormData
	Max          int
	Min          int
	Required     bool
	Disabled     bool
	Options      []*Option
	Autocomplete string

	// FORMAT: "%s is required"
	ErrorMessageFieldRequired string
	// FORMAT: "%s is too long"
	ErrorMessageFieldMax string
	// FORMAT: "%s is too short"
	ErrorMessageFieldMin string
	// FORMAT: "%s is not a valid number (%s)"
	ErrorMessageNaN string

	Validators []validators.Validator

	FormErrors FormErrors

	// Render function
	RenderLabel func(f *Field) Element
	Render      func(f *Field) Element
}

func GenerateFieldsFromStruct

func GenerateFieldsFromStruct(s interface{}) ([]*Field, error)

func (*Field) AddError added in v1.0.6

func (f *Field) AddError(err error)

func (*Field) Clear added in v1.0.6

func (f *Field) Clear()

func (*Field) Errors

func (f *Field) Errors() []FormError

func (*Field) Field

func (f *Field) Field() ElementInterface

func (*Field) GetName added in v1.0.6

func (f *Field) GetName() string

func (*Field) HasError added in v1.0.6

func (f *Field) HasError() bool

func (*Field) HasLabel added in v1.0.6

func (f *Field) HasLabel() bool

func (*Field) IsFile added in v1.0.6

func (f *Field) IsFile() bool

func (*Field) Label

func (f *Field) Label() ElementInterface

func (*Field) SetDisabled added in v1.0.6

func (f *Field) SetDisabled(disabled bool)

func (*Field) SetFile added in v1.0.6

func (f *Field) SetFile(filename string, file io.ReadCloser) error

func (*Field) SetHidden added in v1.0.6

func (f *Field) SetHidden(hidden bool)

func (*Field) SetReadOnly added in v1.0.6

func (f *Field) SetReadOnly(readOnly bool)

func (*Field) SetRequired added in v1.0.6

func (f *Field) SetRequired(required bool)

func (*Field) SetValue added in v1.0.6

func (f *Field) SetValue(value string)

func (*Field) String

func (f *Field) String() string

func (*Field) Validate

func (f *Field) Validate() error

func (*Field) Value

func (f *Field) Value() *FormData

type Form

type Form struct {
	Fields      []FormElement
	Errors      FormErrors
	BeforeValid func(*request.Request, *Form) error
	AfterValid  func(*request.Request, *Form) error
}

func (*Form) AddError

func (f *Form) AddError(name string, err error)

AddError adds an error to the form

func (*Form) AddFields

func (f *Form) AddFields(field ...FormElement)

AddField adds a field to the form

func (Form) AsP

func (f Form) AsP() template.HTML

func (*Form) Clear

func (f *Form) Clear()

func (*Form) Disabled

func (f *Form) Disabled(names ...string) Form

func (*Form) Field

func (f *Form) Field(name string) FormElement

func (*Form) Fill

func (f *Form) Fill(r *request.Request) bool

func (*Form) Get

func (f *Form) Get(name string) *FormData

func (*Form) Validate

func (f *Form) Validate() bool

func (*Form) Without

func (f *Form) Without(names ...string)

type FormData added in v1.0.6

type FormData struct {
	Val      string
	FileName string
	Reader   io.ReadCloser
}

func NewValue added in v1.0.6

func NewValue(s string) *FormData

func (*FormData) File added in v1.0.6

func (f *FormData) File() (string, io.ReadCloser)

func (*FormData) IsFile added in v1.0.6

func (f *FormData) IsFile() bool

func (*FormData) Value added in v1.0.6

func (f *FormData) Value() string

type FormElement added in v1.0.6

type FormElement interface {
	// Get the name of the field.
	GetName() string

	// Whether the field has a label.
	HasLabel() bool
	// Get the label for the field.
	Label() ElementInterface

	// Get the field element.
	Field() ElementInterface

	// Get, set or clear the value of the field.
	SetValue(string)
	SetFile(filename string, file io.ReadCloser) error
	Value() *FormData
	Clear()

	// Validate the field.
	Validate() error

	// Errors
	Errors() []FormError
	AddError(error)
	HasError() bool

	// Relevant attributes to set.
	SetReadOnly(bool)
	SetDisabled(bool)
	SetRequired(bool)
	SetHidden(bool)

	IsFile() bool
}

type FormError

type FormError struct {
	Name     string
	FieldErr error
}

func (FormError) Error

func (f FormError) Error() string

type FormErrors

type FormErrors []FormError

func (*FormErrors) Add

func (f *FormErrors) Add(name string, err error)

func (FormErrors) AsP

func (f FormErrors) AsP() template.HTML

func (FormErrors) AsUL

func (f FormErrors) AsUL() template.HTML

func (FormErrors) Error

func (f FormErrors) Error() string

func (FormErrors) HasErrors

func (f FormErrors) HasErrors() bool

func (FormErrors) String

func (f FormErrors) String() string

type FormValue

type FormValue interface {
	FormValue(name string) string
}

type Option

type Option struct {
	Value *FormData
	Text  *FormData
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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