Documentation ¶
Overview ¶
Form Rendering and Validation System.
Usage
package main import ( "fmt" "io" "mime/multipart" "net/http" "github.com/cjtoolkit/form" _ "github.com/cjtoolkit/form/lang/enGB" ) type TestForm struct { Text string File *multipart.FileHeader } func (t *TestForm) CJForm(f *form.Fields) { // Text func() { f := f.Init(&t.Text, "Text", form.InputText) html := f.HTML() html.Before = "<h1>File Test</h1>" }() // File func() { f := f.Init(&t.File, "File", form.InputFile) file := f.File() file.Size = 10 * 1024 * 1024 file.Accept = []string{"image/jpeg", "image/png"} }() } func main() { mux := http.NewServeMux() mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { f := &TestForm{} v := form.New(req, nil, "en-GB") get := func() { res.Header().Set("Content-Type", "text/html") fmt.Fprint(res, `<form action="/" method="post" enctype="multipart/form-data"> `) v.Render(res, f) fmt.Fprint(res, `<input type="submit" name="submit" value="Submit"> </form>`) } switch req.Method { case "GET": get() case "POST": req.ParseMultipartForm(10 * 1024 * 1024) if !v.MustValidate(f) && f.File == nil { get() return } file, _ := f.File.Open() io.Copy(res, file) } }) http.ListenAndServe(":8080", mux) }
Index ¶
- func RenderAttr(attr map[string]string) (str string)
- func StartingDayOfWeek(year, week int, loc *time.Location) time.Time
- func WeekInAYear(year int, loc *time.Location) int
- type Field
- type FieldFuncs
- func (fns FieldFuncs) Attr(attr map[string]string)
- func (fns FieldFuncs) Call(name string, m map[string]interface{})
- func (fns FieldFuncs) Custom(fn func(*error, *string))
- func (fns FieldFuncs) EmailError(err string)
- func (fns FieldFuncs) File() *File
- func (fns FieldFuncs) HTML() *HTML
- func (fns FieldFuncs) Label() *Label
- func (fns FieldFuncs) Mandatory() *string
- func (fns FieldFuncs) MustMatch() *MustMatch
- func (fns FieldFuncs) Options(v interface{})
- func (fns FieldFuncs) Pattern(re *regexp.Regexp) *string
- func (fns FieldFuncs) Radios(v interface{})
- func (fns FieldFuncs) RangeFloat() *RangeFloat
- func (fns FieldFuncs) RangeInt() *RangeInt
- func (fns FieldFuncs) RangeTime() *RangeTime
- func (fns FieldFuncs) Size() *Size
- func (fns FieldFuncs) StepFloat(f float64) *string
- func (fns FieldFuncs) StepInt(i int64) *string
- func (fns FieldFuncs) Textarea(rows, cols int)
- type Fields
- type File
- type FirstLayer
- type FirstLayerInput
- type FirstLayerSelect
- type FirstLayerStack
- type FirstLayerTextarea
- type Form
- type FormPtr
- type HTML
- type Label
- type MustMatch
- type Option
- type OptionFloat
- type OptionInt
- type Radio
- type RadioFloat
- type RadioInt
- type RangeFloat
- type RangeInt
- type RangeTime
- type RenderData
- type RenderSecondLayer
- type RenderSecondLayerFunc
- type Size
- type TypeCode
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RenderAttr ¶
Render Attribute into strings.
func StartingDayOfWeek ¶
Get time with the starting day of that week in that year!
Types ¶
type FieldFuncs ¶
For keeping a list of enclosed functions for struct pointer fields.
func (FieldFuncs) Call ¶
func (fns FieldFuncs) Call(name string, m map[string]interface{})
Attemp to call a function in FieldFuncs. Does not call if function does not exist.
func (FieldFuncs) Options ¶
func (fns FieldFuncs) Options(v interface{})
Append Options, accept []Option, []OptionInt, []OptionFloat
func (FieldFuncs) Pattern ¶
func (fns FieldFuncs) Pattern(re *regexp.Regexp) *string
Regular Expression
func (FieldFuncs) Radios ¶
func (fns FieldFuncs) Radios(v interface{})
Append Radios, accept []Radio, []RadioInt, []RadioFloat
func (FieldFuncs) StepFloat ¶
func (fns FieldFuncs) StepFloat(f float64) *string
Number of Step Float64
type Fields ¶
Fields
type FirstLayerInput ¶
First Layer Input
type FirstLayerSelect ¶
First Layer Select
type FirstLayerTextarea ¶
First Layer Textarea
type Form ¶
type Form interface { // Renders 'formPtr' to 'w', panic if formPtr is not a struct with pointer. // Also renders validation errors if 'Validate' or 'MustValidate' was call before hand. Render(w io.Writer, formPtr FormPtr) // As Render but Return string RenderStr(formPtr FormPtr) string // Validate User Input and Populate Field in struct with pointers. // Must use struct with pointers otherwise it will return an error. // r cannot be 'nil' Validate(formPtrs ...FormPtr) (bool, error) // Same as validate but panic on error. MustValidate(formPtrs ...FormPtr) bool // Validate Single Field, won't work with must match. ValidateSingle(formPtr FormPtr, name string, value []string) (err error) // Encode JSON into 'w' // {"valid": bool, "data":[{"valid":bool, "error":"", "warning":"", "name":"", "count":int}...]} // Must call Validate or MustValidate first, otnilherwise it's print invalid data. Json(w io.Writer) // Set Location Location(loc *time.Location) }
Form Renderer and Validator interface!
func New ¶
func New(r *http.Request, rsl RenderSecondLayer, languageSources ...string) Form
Create new form validator and renderer. Panic if unable to verify languageSources or r is nil To use Default Second Layer specify rsl as 'nil'.
Note: Stick to one instant per user request, do not use it as a global variable, as it's not thread safe.
type Option ¶
type Option struct { Content string Value string Label string Selected bool Attr map[string]string }
For use with Select (string, []string)
Example ¶
return FieldFuncs{ "form": func(m map[string]interface{}) { *(m["type"].(*TypeCode)) = Select }, "radio": func(m map[string]interface{}) { *(m["radio"].(*[]Option)) = []Option{ {Content: "Car", Value: "car", Label: "Car"}, {Content: "Motorbike", Value: "motorbike", Label: "Motorbike"}, } }, }
Output:
type OptionFloat ¶
type OptionFloat struct { Content string Value float64 Label string Selected bool Attr map[string]string }
For use with Select (float64, []float64)
Example ¶
return FieldFuncs{ "form": func(m map[string]interface{}) { *(m["type"].(*TypeCode)) = Select }, "radio": func(m map[string]interface{}) { *(m["radio"].(*[]OptionFloat)) = []OptionFloat{ {Content: "Car", Value: 1.5, Label: "Car"}, {Content: "Motorbike", Value: 2.5, Label: "Motorbike"}, } }, }
Output:
type OptionInt ¶
type OptionInt struct { Content string Value int64 Label string Selected bool Attr map[string]string }
For use with Select (int64, []int64)
Example ¶
return FieldFuncs{ "form": func(m map[string]interface{}) { *(m["type"].(*TypeCode)) = Select }, "radio": func(m map[string]interface{}) { *(m["radio"].(*[]OptionInt)) = []OptionInt{ {Content: "Car", Value: 1, Label: "Car"}, {Content: "Motorbike", Value: 2, Label: "Motorbike"}, } }, }
Output:
type Radio ¶
For use with InputRadio (string)
Example ¶
return FieldFuncs{ "form": func(m map[string]interface{}) { *(m["type"].(*TypeCode)) = InputRadio }, "radio": func(m map[string]interface{}) { *(m["radio"].(*[]Radio)) = []Radio{ {Value: "car", Label: "Car"}, {Value: "motorbike", Label: "Motorbike"}, } }, }
Output:
type RadioFloat ¶
For use with InputRadio (float64)
Example ¶
return FieldFuncs{ "form": func(m map[string]interface{}) { *(m["type"].(*TypeCode)) = InputRadio }, "radio": func(m map[string]interface{}) { *(m["radio"].(*[]RadioFloat)) = []RadioFloat{ {Value: 1.5, Label: "Car"}, {Value: 2.5, Label: "Motorbike"}, } }, }
Output:
type RadioInt ¶
For use with InputRadio (int64)
Example ¶
return FieldFuncs{ "form": func(m map[string]interface{}) { *(m["type"].(*TypeCode)) = InputRadio }, "radio": func(m map[string]interface{}) { *(m["radio"].(*[]RadioInt)) = []RadioInt{ {Value: 1, Label: "Car"}, {Value: 2, Label: "Motorbike"}, } }, }
Output:
type RangeFloat ¶
Range of Float Field
type RenderData ¶
type RenderData struct { Name string Count int Type TypeCode Error error Warning string Fns FieldFuncs Check bool FirstLayerStacks FirstLayerStack }
For use with RenderSecondLayer
type RenderSecondLayer ¶
type RenderSecondLayer interface {
Render(w io.Writer, r RenderData)
}
Second Layer interface
var DefaultRenderSecondLayer RenderSecondLayer = RenderSecondLayerFunc(defaultRenderSecondLayer)
Default Second Layer
type RenderSecondLayerFunc ¶
type RenderSecondLayerFunc func(w io.Writer, r RenderData)
An function adapter for 'RenderSecondLayer'
func (RenderSecondLayerFunc) Render ¶
func (render RenderSecondLayerFunc) Render(w io.Writer, r RenderData)
A method that just calls the function.
type TypeCode ¶
type TypeCode uint8
For expressing form input type, eg. <input type="text"... , <textarea....
const ( Invalid TypeCode = iota InputCheckbox InputColor InputDate InputDatetime InputDatetimeLocal InputEmail InputFile InputHidden InputMonth InputNumber InputPassword InputRadio InputRange InputSearch InputTel InputText InputTime InputUrl InputWeek Select Textarea )
List of avaliable form input type, 'Invalid' does not count!
Source Files ¶
- doc.go
- fields.go
- fields_sugar.go
- form.go
- interface.go
- misc.go
- option.go
- radio.go
- render.go
- render_firstlayer.go
- render_input_checkbox.go
- render_input_color.go
- render_input_email.go
- render_input_file.go
- render_input_number.go
- render_input_radio.go
- render_input_text.go
- render_input_time.go
- render_secondlayer.go
- render_select.go
- render_textarea.go
- type.go
- validate.go
- validate_input_checkbox.go
- validate_input_color.go
- validate_input_email.go
- validate_input_file.go
- validate_input_number.go
- validate_input_radio.go
- validate_input_text.go
- validate_input_time.go
- validate_select.go
- validate_single.go
- validate_textarea.go
- value.go
Directories ¶
Path | Synopsis |
---|---|
Form Util: similar to ParseForm, ParseMultipartForm from http.Request, except it's cohesive rather than coupled, Request Body and Url Query string are kept separate.
|
Form Util: similar to ParseForm, ParseMultipartForm from http.Request, except it's cohesive rather than coupled, Request Body and Url Query string are kept separate. |
lang
|
|
enGB
English (British)
|
English (British) |
enUS
English (American)
|
English (American) |
secondlayer
|
|
bootstrap
Bootstrap Second Layer (http://getbootstrap.com/)
|
Bootstrap Second Layer (http://getbootstrap.com/) |
data
Data Harvest.
|
Data Harvest. |
foundation
Foundation Second Layer (http://foundation.zurb.com/)
|
Foundation Second Layer (http://foundation.zurb.com/) |