form

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

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ENC_TYPE_URLENCODED = "application/x-www-form-urlencoded"
	ENC_TYPE_MULTIPART  = "multipart/form-data"
)
View Source
const (
	Initialized = iota
	Prepared
	Submitted
	Validated
	Processed
)

Variables

View Source
var (
	ErrInvalidType  = errors.New("value does not match the expected type")
	ErrConvertValue = errors.New("unable to convert value to the correct type")
	ErrNoOptions    = errors.New("no options found")
)
View Source
var (
	ErrNoValue              = errors.New("unable to find the value")
	ErrInvalidSubmittedData = errors.New("invalid submitted data")
)
View Source
var (
	ErrValidationError   = errors.New("the form is not valid")
	ErrRequiredValidator = errors.New("the value is required")
	ErrEmailValidator    = errors.New("the value is not a valid email")
	ErrUrlValidator      = errors.New("the value is not a valid url")
	ErrMaxValidator      = errors.New("the value is too big")
	ErrMinValidator      = errors.New("the value is too small")
	ErrOptionsValidator  = errors.New("the value is not in the list of options")
)
View Source
var (
	ErrInvalidState = errors.New("invalid state")
)
View Source
var (
	ErrNoTemplate = errors.New("unable to find the template to render")
)

Functions

func AttachValues

func AttachValues(form *Form) error

Iterate over all submitted values and assign them to the related data structure if possible. This will not work work if the form has some errors.

func BindUrlValues

func BindUrlValues(form *Form, values url.Values) error

Iterate over all fields and bind the submitted values to the SubmittedValue field in the form. This will not attach the value to the underlying data structure, use AttachValues for this.

This will call all unmarshallers defined on each form field.

func BoolToStr

func BoolToStr(v interface{}) (string, bool)

func Configure

func Configure(l *goapp.Lifecycle, conf *config.Config)

func GetEmbedFS

func GetEmbedFS() embed.FS

func NumberToStr

func NumberToStr(v interface{}) (string, bool)

func PrepareForm

func PrepareForm(form *Form) error

Iterate over all fields and call the marshaller function to transform the Go value into a serialized value used in the HTML form. This also setup the different attribures required by the HTML form.

func Process

func Process(form *Form, request *http.Request) error

func StrToBool

func StrToBool(value interface{}) (bool, bool)

func StrToNumber

func StrToNumber(value string, kind reflect.Kind) (interface{}, bool)

func StrToValue

func StrToValue(value interface{}, src reflect.Value) (interface{}, bool)

func Val

func Val[T FormTypes](form *Form, name string) T

func ValidateForm

func ValidateForm(form *Form) error

func ValueToStr

func ValueToStr(value interface{}, src reflect.Value) (string, bool)

func ValueToStrSlice

func ValueToStrSlice(value interface{}, src reflect.Value) ([]string, bool)

Types

type Attributes

type Attributes map[string]string

type Error

type Error struct {
	Template string
	Class    string
	Style    string
	Value    string
}

type FieldCollectionOptions

type FieldCollectionOptions struct {
	Items     []*FieldCollectionValue
	Configure func(value interface{}) *Form
}

type FieldCollectionValue

type FieldCollectionValue struct {
	Value interface{}
	Key   string
}

type FieldOption

type FieldOption struct {
	Label   string
	Value   interface{}
	Id      string
	Checked bool
}

type FieldOptions

type FieldOptions []*FieldOption

type Form

type Form struct {
	Data      interface{}
	Fields    []*FormField
	HasErrors bool

	Locale  string
	EncType string
	Method  string
	Action  string
	State   int
	// contains filtered or unexported fields
}

func CreateForm

func CreateForm(data interface{}) *Form

func (*Form) Add

func (f *Form) Add(name string, options ...interface{}) *FormField

func (*Form) Get

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

func (*Form) Value

func (f *Form) Value(name string) interface{}

type FormField

type FormField struct {
	Prefix         string // used for nested forms
	Name           string
	Module         string
	Help           string
	Attributes     Attributes
	Label          Label
	Input          Input
	Error          Error
	Mandatory      bool
	InitialValue   interface{}
	SubmittedValue interface{}
	Children       []*FormField
	Touched        bool
	Submitted      bool
	Errors         []string
	HasErrors      bool
	Validators     []Validator
	Marshaller     Marshaller   // from go to serialized
	Unmarshaller   Unmarshaller // from serialized to go

	Options interface{}
	// contains filtered or unexported fields
}

func CreateFormField

func CreateFormField() *FormField

func (*FormField) Add

func (f *FormField) Add(name string, options ...interface{}) *FormField

func (*FormField) AddValidator

func (f *FormField) AddValidator(validator Validator) *FormField

func (*FormField) AddValidators

func (f *FormField) AddValidators(validators ...Validator) *FormField

func (*FormField) Get

func (f *FormField) Get(name string) *FormField

func (*FormField) RemoveValidator

func (f *FormField) RemoveValidator(code string) *FormField

func (*FormField) ResetValidators

func (f *FormField) ResetValidators() *FormField

func (*FormField) SetAutocomplete

func (f *FormField) SetAutocomplete(value string) *FormField

func (*FormField) SetAutocorrect

func (f *FormField) SetAutocorrect(value bool) *FormField

func (*FormField) SetAutofocus

func (f *FormField) SetAutofocus(value bool) *FormField

func (*FormField) SetChecked

func (f *FormField) SetChecked(value bool) *FormField

func (*FormField) SetClass

func (f *FormField) SetClass(value string) *FormField

func (*FormField) SetHeight

func (f *FormField) SetHeight(value uint16) *FormField

func (*FormField) SetHelp

func (f *FormField) SetHelp(value string) *FormField

func (*FormField) SetList

func (f *FormField) SetList(value string) *FormField

func (*FormField) SetMarshaller

func (f *FormField) SetMarshaller(marshaller Marshaller) *FormField

func (*FormField) SetMax

func (f *FormField) SetMax(max interface{}) *FormField

func (*FormField) SetMaxLength

func (f *FormField) SetMaxLength(value uint32) *FormField

func (*FormField) SetMin

func (f *FormField) SetMin(min interface{}) *FormField

func (*FormField) SetMinLength

func (f *FormField) SetMinLength(value uint32) *FormField

func (*FormField) SetModule

func (f *FormField) SetModule(value string) *FormField

func (*FormField) SetMultiple

func (f *FormField) SetMultiple(value bool) *FormField

func (*FormField) SetNovalidation

func (f *FormField) SetNovalidation(value bool) *FormField

func (*FormField) SetPattern

func (f *FormField) SetPattern(value string) *FormField

func (*FormField) SetPlaceholder

func (f *FormField) SetPlaceholder(value string) *FormField

func (*FormField) SetReadonly

func (f *FormField) SetReadonly(value bool) *FormField

func (*FormField) SetRequired

func (f *FormField) SetRequired(value bool) *FormField

func (*FormField) SetSize

func (f *FormField) SetSize(value uint16) *FormField

func (*FormField) SetStep

func (f *FormField) SetStep(value uint32) *FormField

func (*FormField) SetTemplate

func (f *FormField) SetTemplate(value string) *FormField

func (*FormField) SetType

func (f *FormField) SetType(value string) *FormField

func (*FormField) SetUnmarshaller

func (f *FormField) SetUnmarshaller(unmarshaller Unmarshaller) *FormField

func (*FormField) SetWidth

func (f *FormField) SetWidth(value uint16) *FormField

func (*FormField) Value

func (f *FormField) Value(name string) interface{}

type FormTypes

type FormTypes interface {
	string | bool | int
}

type Input

type Input struct {
	Name         string
	Template     string
	Class        string
	Style        string
	Value        string
	Placeholder  string // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#placeholder
	Type         string
	Id           string
	Pattern      string // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#pattern
	List         string
	Autocomplete string // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#autocomplete
	Spellcheck   string
	Readonly     bool
	Disabled     bool
	Checked      bool
	Multiple     bool
	Required     bool
	Autofocus    bool
	Autocorrect  bool
	Novalidate   bool
	Size         uint16
	MinLength    uint32
	MaxLength    uint32
	Min          interface{}
	Max          interface{}
	Step         uint32
	Height       uint16
	Width        uint16
	Options      FieldOptions
}

type Label

type Label struct {
	Template string
	Class    string
	Style    string
	Value    string
}

type Marshaller

type Marshaller func(field *FormField, form *Form) error

type MarshallerResult

type MarshallerResult struct {
	Marshaller   Marshaller
	Unmarshaller Unmarshaller
	Type         string
}

type Unmarshaller

type Unmarshaller func(field *FormField, form *Form, values url.Values) error

type Validator

type Validator interface {
	Validate(field *FormField, form *Form) error
	Code() string
}

func EmailValidator

func EmailValidator() Validator

func MaxLengthValidator

func MaxLengthValidator(max uint32, mode string) Validator

func MaxValidator

func MaxValidator[T number](max T) Validator

func MinLengthValidator

func MinLengthValidator(min uint32, mode string) Validator

func MinValidator

func MinValidator[T number](min T) Validator

func OptionsValidator

func OptionsValidator() Validator

func RequiredValidator

func RequiredValidator() Validator

func UrlValidator

func UrlValidator() Validator

type ValidatorFunc

type ValidatorFunc struct {
	// contains filtered or unexported fields
}

func (*ValidatorFunc) Code

func (v *ValidatorFunc) Code() string

func (*ValidatorFunc) Validate

func (v *ValidatorFunc) Validate(field *FormField, form *Form) error

Jump to

Keyboard shortcuts

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