定制错误信息 ~~~ |-- admin | |--controllers | `-- user.go ~~~ ~~~ package admin import ( "fmt" "github.com/astaxie/beego" "github.com/astaxie/beego/validation" ) type UserController struct { beego.Controller } type User struct { Name string Age int } func (this *UserController) Index() { u := User{"man", 40} valid := validation.Validation{} valid.Range(u.Age, 0, 18, "age") // 定制错误信息 minAge := 18 valid.Max(u.Age, minAge, "age").Message("少儿不宜!") // 错误信息格式化 valid.Max(u.Age, minAge, "age").Message("%d不禁", minAge) if valid.HasErrors() { // 如果有错误信息,证明验证没通过 // 打印错误信息 for _, err := range valid.Errors { this.Ctx.WriteString(fmt.Sprintln(err.Key, err.Message)) } } } ~~~ 浏览器访问: http://127.0.0.1:8080/admin/user/index 浏览器输出: ~~~ age Range is 0 to 18 age 少儿不宜! age 18不禁 ~~~