Documentation
¶
Overview ¶
Package Form specifies the functions required to create and validate forms. This is helpful to add server-side validations.
Index ¶
- Constants
- Variables
- type Field
- type FieldBuilder
- func (fb *FieldBuilder) Build() *Field
- func (fb *FieldBuilder) Empty(value interface{}) *FieldBuilder
- func (fb *FieldBuilder) FieldType(value string) *FieldBuilder
- func (fb *FieldBuilder) Label(value string) *FieldBuilder
- func (fb *FieldBuilder) Loader(loader LoaderFunc) *FieldBuilder
- func (fb *FieldBuilder) Max(value int) *FieldBuilder
- func (fb *FieldBuilder) Min(value int) *FieldBuilder
- func (fb *FieldBuilder) Required() *FieldBuilder
- func (fb *FieldBuilder) WithFormatters(formatters ...FormatterFunc) *FieldBuilder
- func (fb *FieldBuilder) WithValidators(validators ...ValidatorFunc) *FieldBuilder
- type Form
- type FormValidator
- type FormValues
- type FormatterFunc
- type LoaderFunc
- type ValidatorFunc
Constants ¶
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.
Variables ¶
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.
var StringLoader = LoaderFunc(func(rawValue string) (interface{}, error) { return rawValue, nil })
StringLoader specifies form data as string.
var TimeLoader = NewTimeLoader(time.RFC3339)
TimeLoader specifies form data as time.
Functions ¶
This section is empty.
Types ¶
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.
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 ¶
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) 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 ¶
FormatterFunc implements the formating functionality on the form field
type LoaderFunc ¶
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