mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 06:00:55 +00:00
Add custom validation function doc
This commit is contained in:
parent
103ac3ee5b
commit
21f767784b
@ -63,7 +63,48 @@ Struct Tag Use:
|
||||
}
|
||||
|
||||
func main() {
|
||||
valid := Validation{}
|
||||
valid := validation.Validation{}
|
||||
u := user{Name: "test", Age: 40}
|
||||
b, err := valid.Valid(u)
|
||||
if err != nil {
|
||||
// handle error
|
||||
}
|
||||
if !b {
|
||||
// validation does not pass
|
||||
// blabla...
|
||||
}
|
||||
}
|
||||
|
||||
Use custom function:
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/validation"
|
||||
)
|
||||
|
||||
type user struct {
|
||||
Id int
|
||||
Name string `valid:"Required;IsMe"`
|
||||
Age int `valid:"Required;Range(1, 140)"`
|
||||
}
|
||||
|
||||
func IsMe(v *validation.Validation, obj interface{}, key string) {
|
||||
name, ok:= obj.(string)
|
||||
if !ok {
|
||||
// wrong use case?
|
||||
return
|
||||
}
|
||||
|
||||
if name != "me" {
|
||||
// valid false
|
||||
v.SetError("Name", "is not me!")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
valid := validation.Validation{}
|
||||
if err := validation.AddCustomFunc("IsMe", IsMe); err != nil {
|
||||
// hadle error
|
||||
}
|
||||
u := user{Name: "test", Age: 40}
|
||||
b, err := valid.Valid(u)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user