1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 00:40:50 +00:00
Beego/validation
2013-07-23 14:42:14 +08:00
..
README.md add validation README 2013-07-22 17:40:32 +08:00
util_test.go add some util funcs 2013-07-23 14:42:14 +08:00
util.go add some util funcs 2013-07-23 14:42:14 +08:00
validation_test.go add validation README 2013-07-22 17:40:32 +08:00
validation.go add test 2013-07-21 23:46:18 +08:00
validators.go add test 2013-07-21 23:46:18 +08:00

validation

validation is a form validation for a data validation and error collecting using Go.

Installation and tests

Install:

go get github.com/astaxie/beego/validation

Test:

go test github.com/astaxie/beego/validation

Example

import (
	"github.com/astaxie/beego/validation"
	"log"
)

type User struct {
	Name string
	Age int
}

func main() {
	u := User{"man", 40}
	valid := validation.Validation{}
	valid.Required(u.Name, "name")
	valid.MaxSize(u.Name, 15, "nameMax")
	valid.Range(u.Age, 0, 140, "age")
	if valid.HasErrors {
		// validation does not pass
		// print invalid message
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}
	// or use like this
	if v := valid.Max(u.Age, 140); !v.Ok {
		log.Println(v.Error.Key, v.Error.Message)
	}
}

LICENSE

BSD License http://creativecommons.org/licenses/BSD/