package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
var (
ctx = gctx.New()
params = map[string]interface{}{
"passport": "",
"password": "123456",
"password2": "1234567",
}
rules = map[string]string{
"passport": "required|length:6,16",
"password": "required|length:6,16|same:password2",
"password2": "required|length:6,16",
}
)
err := g.Validator().Rules(rules).Data(params).Run(ctx)
if err != nil {
g.Dump(err.Maps())
}
}
执行后,终端输出:
{
"passport": {
"required": "The passport field is required",
"length": "The passport value `` length must be between 6 and 16",
},
"password": {
"same": "The password value `123456` must be the same as field password2",
},
}
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
var (
ctx = gctx.New()
params = map[string]interface{}{
"passport": "",
"password": "123456",
"password2": "1234567",
}
rules = map[string]string{
"passport": "required|length:6,16",
"password": "required|length:6,16|same:password2",
"password2": "required|length:6,16",
}
messages = map[string]interface{}{
"passport": "账号不能为空|账号长度应当在{min}到{max}之间",
"password": map[string]string{
"required": "密码不能为空",
"same": "两次密码输入不相等",
},
}
)
err := g.Validator().Messages(messages).Rules(rules).Data(params).Run(ctx)
if err != nil {
g.Dump(err.Maps())
}
}
该示例同时也展示了messsages
自定义错误信息传递的两种数据类型,string
或者map[string]string
。其中map[string]string
类型参数需要指定对应字段、对应规则的错误提示信息,是一个二维的“关联数组”。该示例执行后,终端输出:
{
"passport": {
"length": "账号长度应当在6到16之间",
"required": "账号不能为空"
},
"password": {
"same": "两次密码输入不相等"
}
}
项目中我们经常会遇到大量struct的使用,以及各种数据类型到struct的转换/赋值(特别是json/xml/各种协议编码转换)...
gredis采用接口化设计,具有强大的灵活性和扩展性。接口定义https://pkg.go.dev/github.com/gogf/gf/v2/database/gredis#Ada...
我们来看一个使用示例,在该示例中,展示了资源管理在WebServer的静态服务、配置管理、模板引擎中的使用。资源文件资源文件...
ASP.NET Panel Wrap 属性 Panel 控件定义和用法 Wrap 属性用于设置或返回在 Panel 控件中内容是否折行。 语法asp:Panel Wrap="TR...