GoFrame Map校验-基本使用

默认错误提示

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": "两次密码输入不相等"
    }
}

作者:terry,如若转载,请注明出处:https://www.web176.com/goframe/21076.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年5月18日
下一篇 2023年5月18日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注


Fatal error: Uncaught JSMin_UnterminatedStringException: JSMin: Unterminated String at byte 410: ", in /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/jsmin.php:214 Stack trace: #0 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/jsmin.php(152): JSMin->action() #1 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/jsmin.php(86): JSMin->min() #2 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/ao-minify-html.php(257): JSMin::minify() #3 [internal function]: AO_Minify_HTML->_removeScriptCB() #4 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/ao-minify-html.php(108): preg_replace_callback() #5 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/ao-minify-html.php(47): AO_Minify_HTML->process() #6 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/autoptimizeHTML.php(105): AO_Minify_HTML::minify() #7 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/aut in /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/jsmin.php on line 214