form

package
v0.0.0-...-b87fc4c Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2020 License: MIT Imports: 5 Imported by: 5

Documentation

Index

Constants

View Source
const (
	MSG_REQUIRED     = "this field is required"
	MSG_MAX_LENGTH   = "length must less than {0}"
	MSG_MIN_LENGTH   = "length must more than {0}"
	MST_RANGE_LENGTH = "length must between {0} and {1}"
	MSG_MAX          = "value must small than {0}"
	MSG_MIN          = "value must large than {0}"
	MSG_RANGE        = "value must between {0} and {1}"
	MSG_INVALID      = "not a valid value"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseField

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

func (*BaseField) CleanValue

func (bf *BaseField) CleanValue() interface{}

func (*BaseField) Error

func (bf *BaseField) Error(errorType string, errorMsg string) *BaseField

The return value is the BaseField, so calls can be chained

func (*BaseField) ErrorMsg

func (bf *BaseField) ErrorMsg() string

func (*BaseField) Field

func (bf *BaseField) Field() Field

convert to Field interface

func (*BaseField) IsValid

func (bf *BaseField) IsValid() bool

func (*BaseField) Max

func (bf *BaseField) Max(max int) *BaseField

The return value is the BaseField, so calls can be chained

func (*BaseField) MaxLength

func (bf *BaseField) MaxLength(max int) *BaseField

The return value is the BaseField, so calls can be chained

func (*BaseField) Min

func (bf *BaseField) Min(min int) *BaseField

The return value is the BaseField, so calls can be chained

func (*BaseField) MinLength

func (bf *BaseField) MinLength(min int) *BaseField

The return value is the BaseField, so calls can be chained

func (*BaseField) Name

func (bf *BaseField) Name() string

func (*BaseField) NickName

func (bf *BaseField) NickName() string

func (*BaseField) Range

func (bf *BaseField) Range(min int, max int) *BaseField

The return value is the BaseField, so calls can be chained

func (*BaseField) Required

func (bf *BaseField) Required(r bool) *BaseField

The return value is the BaseField, so calls can be chained

func (*BaseField) SetValue

func (bf *BaseField) SetValue(val string)

func (*BaseField) Valid

func (bf *BaseField) Valid() *ValidResult

func (*BaseField) Value

func (bf *BaseField) Value() string

type CharField

type CharField struct {
	BaseField
}

func NewCharField

func NewCharField(name string, nickname string, required bool) *CharField

type EmailField

type EmailField struct {
	RegexpField
}

func NewEmailField

func NewEmailField(name string, nickname string, required bool) *EmailField

type Field

type Field interface {
	Name() string
	NickName() string
	ErrorMsg() string
	Value() string
	SetValue(s string)
	CleanValue() interface{}
	IsValid() bool
	Valid() *ValidResult
}

type FieldOption

type FieldOption struct {
	Required  bool
	NotTrim   bool // not trim the Whitespace
	Range     [2]int
	ErrorMsgs map[string]string
}

type Form

type Form struct {
	Name    string
	Action  string
	Fields  map[string]Field
	IsValid bool
}

func NewForm

func NewForm(fields ...Field) *Form

create a new form whit fields

func (*Form) CleanValues

func (fm *Form) CleanValues() map[string]interface{}

CleanValues gets the clean value after validated before you get fm.ClentValues(), you must call fm.Valid() first.

func (*Form) Errors

func (fm *Form) Errors() map[string][]string

func (*Form) FillByMap

func (fm *Form) FillByMap(m map[string]string)

func (*Form) FillByRequest

func (fm *Form) FillByRequest(req *http.Request)

func (*Form) Valid

func (fm *Form) Valid() bool

Valid checks all the values on the field are validated, if validated, set the clean value

func (*Form) Values

func (fm *Form) Values() map[string]string

Values gets the source values the values are the same as the submit on the request

type IntegerField

type IntegerField struct {
	BaseField
}

func NewIntegerField

func NewIntegerField(name string, nickname string, required bool) *IntegerField

type RegexpField

type RegexpField struct {
	BaseField
	// contains filtered or unexported fields
}

func NewRegexpField

func NewRegexpField(name string, nickname string, required bool, re string) *RegexpField

type TextField

type TextField struct {
	BaseField
}

func NewTextField

func NewTextField(name string, nickname string, required bool) *TextField

type ValidOption

type ValidOption struct {
	Required bool
	NotTrim  bool // not trim the Whitespace
	// Max      int
	// Min      int
	Range    [2]int
	ErrorMsg string
}

type ValidResult

type ValidResult struct {
	IsValid    bool
	ErrorMsg   string
	CleanValue interface{}
}

type Validater

type Validater interface {
	Valid(source string, option *FieldOption) *ValidResult
}

Jump to

Keyboard shortcuts

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