Documentation
¶
Overview ¶
Package github.com/ToQoz/formspec validates a form. So it will expresses **spec** for form. This is generally used in http.Handler, but you can use *formspec.Result as a retuen value of your validation func in models.
ExampleApp: https://github.com/ToQoz/go-formspec/tree/master/_example
Simple usage in http.Handler.
package main import ( "fmt" "github.com/ToQoz/go-formspec" "log" "net/http" ) func main() { s := formspec.New() s.Rule("name", formspec.RuleRequired()) s.Rule("age", formspec.RuleInt()).Message("must be integer. ok?").AllowBlank() s.Rule("nick", formspec.RuleRequired()).FullMessage("Please enter your cool nickname.") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { vr := s.Validate(r) if !vr.Ok { w.WriteHeader(403) for _, verr := range vr.Errors { w.Write([]byte(fmt.Sprintf("Validation error in %s. %s\n", verr.Field, verr.Message))) } return } w.Write([]byte("ok")) }) log.Fatal(http.ListenAndServe(":8888", nil)) }
Index ¶
- Variables
- type Error
- type FilterFunc
- type Form
- type Formspec
- type Result
- type Rule
- type RuleFunc
- func RuleFloatGreaterThan(a float64) RuleFunc
- func RuleFloatLessThan(a float64) RuleFunc
- func RuleFormat(r *regexp.Regexp) RuleFunc
- func RuleInt() RuleFunc
- func RuleIntGreaterThan(a int) RuleFunc
- func RuleIntLessThan(a float64) RuleFunc
- func RuleMaxLen(maxLen int) RuleFunc
- func RuleMinLen(minLen int) RuleFunc
- func RuleNumber() RuleFunc
- func RuleRequired() RuleFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // regexp for number format. You change override. RuleFormatNumber = regexp.MustCompile(`\A[+-]?\d+\.?\d*\z`) // regexp for integer format. You change override. RuleFormatInt = regexp.MustCompile(`\A[+-]?\d+\z`) RuleMessageRequired = "is required." RuleMessageMaxLen = "is too long. Max is %d character." RuleMessageMinLen = "is too short. Min is %d character." RuleInvalidMessage = "is invalid." RuleMessageNumber = "must be number." RuleMessageInt = "must be integer." RuleMessageLessThan = "must be less than %d" RuleMessageGreaterThan = "must be greater than %d" )
Functions ¶
This section is empty.
Types ¶
type FilterFunc ¶
type Formspec ¶
type Formspec struct {
Rules []*Rule
}
Example (Basic) ¶
package main import ( "fmt" "github.com/ToQoz/go-formspec" ) type exampleForm struct { form map[string]string } func (e *exampleForm) Set(key, value string) { e.form[key] = value } func (e *exampleForm) FormValue(value string) string { return "" } func main() { aFormspec := formspec.New() aFormspec.Rule("name", formspec.RuleRequired()) aFormspec.Rule("age", formspec.RuleInt()).Message("must be integer. ok?").AllowBlank() aFormspec.Rule("nick", formspec.RuleRequired()).FullMessage("Please enter your cool nick.") f := &exampleForm{} // f.Set("name", "ToQoz") f.Set("age", "invalid int") // f.Set("age", "22") // f.Set("nick", "Toqoz") vr := aFormspec.Validate(f) if !vr.Ok { for _, verr := range vr.Errors { fmt.Printf("Validation error in %s. Message is %s.\n", verr.Field, verr.Message) } } }
Output:
type Result ¶
func NewNgResult ¶
func NewNgResult() *Result
func NewOkResult ¶
func NewOkResult() *Result
type Rule ¶
type Rule struct { Field string RuleFunc RuleFunc FilterFuncs []FilterFunc // contains filtered or unexported fields }
func (*Rule) AllowBlank ¶
func (*Rule) Filter ¶
func (r *Rule) Filter(filterFunc FilterFunc) *Rule
func (*Rule) FullMessage ¶
FullMessage sets Rule.fullMessage.
type RuleFunc ¶
func RuleFloatGreaterThan ¶
func RuleFloatLessThan ¶
func RuleFormat ¶
func RuleIntGreaterThan ¶
func RuleIntLessThan ¶
func RuleMaxLen ¶
func RuleMinLen ¶
func RuleNumber ¶
func RuleNumber() RuleFunc
func RuleRequired ¶
func RuleRequired() RuleFunc
Click to show internal directories.
Click to hide internal directories.