Version: v1.0.1 Latest Latest

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

Go to latest
Published: Jul 13, 2020 License: GPL-3.0 Imports: 9 Imported by: 0



Package Form specifies the functions required to create and validate forms. This is helpful to add server-side validations.



View Source
const (
	TEXT     = "text"
	TEXTAREA = "textarea"
	PASSWORD = "password"
	EMAIL    = "email"
	CHECKBOX = "checkbox"
	BUTTON   = "button"

The below constant block contains the list of form field types. These are used in FieldBuilder and Field struct.


View Source
var IntLoader = LoaderFunc(func(rawValue string) (interface{}, error) {
	val, err := strconv.ParseInt(rawValue, 0, 0)
	if err != nil {
		return nil, err
	return int(val), nil

IntLoader specifies form data as int.

View Source
var StringLoader = LoaderFunc(func(rawValue string) (interface{}, error) {
	return rawValue, nil

StringLoader specifies form data as string.

TimeLoader specifies form data as time.


This section is empty.


type Field

type Field struct {
	Name       string
	Formatters []FormatterFunc
	Loader     LoaderFunc
	Validators []ValidatorFunc
	Required   bool
	Empty      interface{}
	Min        int
	Max        int
	Label      string
	FieldType  string

Field represents an element of a form. For instance, text, password field.

func (*Field) Validate

func (f *Field) Validate(rawValue string) (interface{}, error)

Validate tests if the data passed in the field follows the specified validation rules.

type FieldBuilder

type FieldBuilder struct {
	// contains filtered or unexported fields

FieldBuilder is a helper to construct the form Field

func (*FieldBuilder) Build

func (fb *FieldBuilder) Build() *Field

Build returns a pointer to Field

func (*FieldBuilder) Empty

func (fb *FieldBuilder) Empty(value interface{}) *FieldBuilder

Empty sets value of empty attribute of the FieldBuilder.

func (*FieldBuilder) FieldType

func (fb *FieldBuilder) FieldType(value string) *FieldBuilder

FieldType sets type (text, textarea, email etc) of the FieldBuilder.

func (*FieldBuilder) Label

func (fb *FieldBuilder) Label(value string) *FieldBuilder

Label sets label of the FieldBuilder.

func (*FieldBuilder) Loader

func (fb *FieldBuilder) Loader(loader LoaderFunc) *FieldBuilder

Loader specifies the name of the loader function to attach the datatype of the field.

func (*FieldBuilder) Max

func (fb *FieldBuilder) Max(value int) *FieldBuilder

Max sets max value acceptable by the FieldBuilder.

func (*FieldBuilder) Min

func (fb *FieldBuilder) Min(value int) *FieldBuilder

Min sets min value acceptable by the FieldBuilder.

func (*FieldBuilder) Required

func (fb *FieldBuilder) Required() *FieldBuilder

Required specifies that the field is mandatory

func (*FieldBuilder) WithFormatters

func (fb *FieldBuilder) WithFormatters(formatters ...FormatterFunc) *FieldBuilder

WithFormatters specifies the name of the formatting function of the field.

func (*FieldBuilder) WithValidators

func (fb *FieldBuilder) WithValidators(validators ...ValidatorFunc) *FieldBuilder

WithValidators specifies the name of the validator function to attach the validator of the field.

type Form

type Form struct {
	Fields     map[string]*Field
	FieldNames []string
	Errors     map[string]error
	Values     FormValues

	PrefixFieldName string
	EncryptType     string
	Action          string
	Method          string
	Name            string
	// contains filtered or unexported fields

Form struct represents an HTML form

func New

func New(name string, prefix string, encryption string, action string, method string) *Form

New returns a pointer to Form It accepts prefix value for each field, encryption to specify if the form accepts multipart data, action specifies the url of form submission, and method (GET/POST) supported by the form

func (*Form) Valid

func (f *Form) Valid(postForm url.Values) bool

Valid validates every field followed by the form's validator if provided.

func (*Form) WithField

func (f *Form) WithField(name string, fb *FieldBuilder) *Form

WithField adds the Field produced by the FieldBuilder to the Form under the given name.

func (*Form) WithValidator

func (f *Form) WithValidator(validator FormValidator) *Form

WithValidator adds the FormValidator to the form.

type FormValidator

type FormValidator func(formValues *FormValues) bool

FormValidator represents a form validator function

type FormValues

type FormValues map[string]interface{}

FormValues represents a map of form field values

type FormatterFunc

type FormatterFunc func(string) string

FormatterFunc implements the formating functionality on the form field

type LoaderFunc

type LoaderFunc func(string) (interface{}, error)

LoaderFunc specifies the datatype of data is stored in a field.

func NewTimeLoader

func NewTimeLoader(layout string) LoaderFunc

type ValidatorFunc

type ValidatorFunc func(interface{}) error

ValidatorFunc specifies the criteria of testing the field data.

func PasswordValidator

func PasswordValidator(errorMsg string) ValidatorFunc

PasswordValidator validates data using valid password criteria in package validators

func RangeValidator

func RangeValidator(min, max int, errorMsg string) ValidatorFunc

RangeValidator validates data using given range of values

func ReValidator

func ReValidator(pattern string, errorMsg string) ValidatorFunc

ReValidator validates data using given regular expression pattern

Jump to

Keyboard shortcuts

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