gvalid_message.go 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gvalid
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/i18n/gi18n"
  10. )
  11. // defaultMessages is the default error messages.
  12. // Note that these messages are synchronized from ./i18n/en/validation.toml .
  13. var defaultMessages = map[string]string{
  14. "required": "The :attribute field is required",
  15. "required-if": "The :attribute field is required",
  16. "required-unless": "The :attribute field is required",
  17. "required-with": "The :attribute field is required",
  18. "required-with-all": "The :attribute field is required",
  19. "required-without": "The :attribute field is required",
  20. "required-without-all": "The :attribute field is required",
  21. "date": "The :attribute value is not a valid date",
  22. "date-format": "The :attribute value does not match the format :format",
  23. "email": "The :attribute value must be a valid email address",
  24. "phone": "The :attribute value must be a valid phone number",
  25. "telephone": "The :attribute value must be a valid telephone number",
  26. "passport": "The :attribute value is not a valid passport format",
  27. "password": "The :attribute value is not a valid passport format",
  28. "password2": "The :attribute value is not a valid passport format",
  29. "password3": "The :attribute value is not a valid passport format",
  30. "postcode": "The :attribute value is not a valid passport format",
  31. "resident-id": "The :attribute value is not a valid resident id number",
  32. "bank-card": "The :attribute value must be a valid bank card number",
  33. "qq": "The :attribute value must be a valid QQ number",
  34. "ip": "The :attribute value must be a valid IP address",
  35. "ipv4": "The :attribute value must be a valid IPv4 address",
  36. "ipv6": "The :attribute value must be a valid IPv6 address",
  37. "mac": "The :attribute value must be a valid MAC address",
  38. "url": "The :attribute value must be a valid URL address",
  39. "domain": "The :attribute value must be a valid domain format",
  40. "length": "The :attribute value length must be between :min and :max",
  41. "min-length": "The :attribute value length must be equal or greater than :min",
  42. "max-length": "The :attribute value length must be equal or lesser than :max",
  43. "between": "The :attribute value must be between :min and :max",
  44. "min": "The :attribute value must be equal or greater than :min",
  45. "max": "The :attribute value must be equal or lesser than :max",
  46. "json": "The :attribute value must be a valid JSON string",
  47. "xml": "The :attribute value must be a valid XML string",
  48. "array": "The :attribute value must be an array",
  49. "integer": "The :attribute value must be an integer",
  50. "float": "The :attribute value must be a float",
  51. "boolean": "The :attribute value field must be true or false",
  52. "same": "The :attribute value must be the same as field :field",
  53. "different": "The :attribute value must be different from field :field",
  54. "in": "The :attribute value is not in acceptable range",
  55. "not-in": "The :attribute value is not in acceptable range",
  56. "regex": "The :attribute value is invalid",
  57. "__default__": "The :attribute value is invalid",
  58. }
  59. // getErrorMessageByRule retrieves and returns the error message for specified rule.
  60. // It firstly retrieves the message from custom message map, and then checks i18n manager,
  61. // it returns the default error message if it's not found in custom message map or i18n manager.
  62. func getErrorMessageByRule(ruleKey string, customMsgMap map[string]string) string {
  63. content := customMsgMap[ruleKey]
  64. if content != "" {
  65. return content
  66. }
  67. content = gi18n.GetContent(fmt.Sprintf(`gf.gvalid.rule.%s`, ruleKey))
  68. if content == "" {
  69. content = defaultMessages[ruleKey]
  70. }
  71. // If there's no configured rule message, it uses default one.
  72. if content == "" {
  73. content = gi18n.GetContent(`gf.gvalid.rule.__default__`)
  74. if content == "" {
  75. content = defaultMessages["__default__"]
  76. }
  77. }
  78. return content
  79. }