2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-11-13 00:12:35 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-11-13 00:12:35 +08:00
|
|
|
|
|
|
|
package gvalid
|
|
|
|
|
2020-05-10 10:56:11 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/i18n/gi18n"
|
|
|
|
)
|
|
|
|
|
2020-05-10 09:05:52 +08:00
|
|
|
// defaultMessages is the default error messages.
|
2019-06-19 09:06:52 +08:00
|
|
|
var defaultMessages = map[string]string{
|
2020-05-10 09:05:52 +08:00
|
|
|
"required": "The :attribute field is required",
|
2020-05-10 16:48:00 +08:00
|
|
|
"required-if": "The :attribute field is required",
|
|
|
|
"required-unless": "The :attribute field is required",
|
|
|
|
"required-with": "The :attribute field is required",
|
|
|
|
"required-with-all": "The :attribute field is required",
|
|
|
|
"required-without": "The :attribute field is required",
|
|
|
|
"required-without-all": "The :attribute field is required",
|
|
|
|
"date": "The :attribute value is not a valid date",
|
|
|
|
"date-format": "The :attribute value does not match the format :format",
|
|
|
|
"email": "The :attribute value must be a valid email address",
|
|
|
|
"phone": "The :attribute value must be a valid phone number",
|
|
|
|
"telephone": "The :attribute value must be a valid telephone number",
|
2020-05-10 10:56:11 +08:00
|
|
|
"passport": "The :attribute value is not a valid passport format",
|
|
|
|
"password": "The :attribute value is not a valid passport format",
|
|
|
|
"password2": "The :attribute value is not a valid passport format",
|
|
|
|
"password3": "The :attribute value is not a valid passport format",
|
|
|
|
"postcode": "The :attribute value is not a valid passport format",
|
|
|
|
"resident-id": "The :attribute value is not a valid resident id number",
|
2020-05-10 16:48:00 +08:00
|
|
|
"bank-card": "The :attribute value must be a valid bank card number",
|
|
|
|
"qq": "The :attribute value must be a valid QQ number",
|
|
|
|
"ip": "The :attribute value must be a valid IP address",
|
|
|
|
"ipv4": "The :attribute value must be a valid IPv4 address",
|
|
|
|
"ipv6": "The :attribute value must be a valid IPv6 address",
|
|
|
|
"mac": "The :attribute value must be a valid MAC address",
|
|
|
|
"url": "The :attribute value must be a valid URL address",
|
|
|
|
"domain": "The :attribute value must be a valid domain format",
|
|
|
|
"length": "The :attribute value length must be between :min and :max",
|
|
|
|
"min-length": "The :attribute value length must be equal or greater than :min",
|
|
|
|
"max-length": "The :attribute value length must be equal or lesser than :max",
|
2020-05-10 10:56:11 +08:00
|
|
|
"between": "The :attribute value must be between :min and :max",
|
|
|
|
"min": "The :attribute value must be equal or greater than :min",
|
|
|
|
"max": "The :attribute value must be equal or lesser than :max",
|
2020-05-10 16:48:00 +08:00
|
|
|
"json": "The :attribute value must be a valid JSON string",
|
|
|
|
"xml": "The :attribute value must be a valid XML string",
|
|
|
|
"array": "The :attribute value must be an array",
|
|
|
|
"integer": "The :attribute value must be an integer",
|
|
|
|
"float": "The :attribute value must be a float",
|
|
|
|
"boolean": "The :attribute value field must be true or false",
|
2020-05-10 22:32:10 +08:00
|
|
|
"same": "The :attribute value must be the same as field :field",
|
|
|
|
"different": "The :attribute value must be different from field :field",
|
2020-05-10 10:56:11 +08:00
|
|
|
"in": "The :attribute value is not in acceptable range",
|
|
|
|
"not-in": "The :attribute value is not in acceptable range",
|
|
|
|
"regex": "The :attribute value is invalid",
|
2018-11-13 00:12:35 +08:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:32:10 +08:00
|
|
|
// getErrorMessageByRule retrieves and returns the error message for specified rule.
|
|
|
|
// It firstly retrieves the message from custom message map, and then checks i18n manager,
|
|
|
|
// it returns the default error message if it's not found in custom message map or i18n manager.
|
|
|
|
func getErrorMessageByRule(ruleKey string, customMsgMap map[string]string) string {
|
|
|
|
content := customMsgMap[ruleKey]
|
|
|
|
if content != "" {
|
|
|
|
return content
|
|
|
|
}
|
2020-05-10 22:34:16 +08:00
|
|
|
content = gi18n.GetContent(fmt.Sprintf(`gf.gvalid.rule.%s`, ruleKey))
|
2020-05-10 10:56:11 +08:00
|
|
|
if content == "" {
|
2020-05-10 22:32:10 +08:00
|
|
|
content = defaultMessages[ruleKey]
|
2020-05-10 10:56:11 +08:00
|
|
|
}
|
|
|
|
return content
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|