gvalid_rule_required.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "strings"
  9. )
  10. // checkRequired checks <value> using required rules.
  11. func checkRequired(value, ruleKey, ruleVal string, params map[string]string) bool {
  12. required := false
  13. switch ruleKey {
  14. // Required.
  15. case "required":
  16. required = true
  17. // Required unless all given field and its value are equal.
  18. // Example: required-if: id,1,age,18
  19. case "required-if":
  20. required = false
  21. array := strings.Split(ruleVal, ",")
  22. // It supports multiple field and value pairs.
  23. if len(array)%2 == 0 {
  24. for i := 0; i < len(array); {
  25. tk := array[i]
  26. tv := array[i+1]
  27. if v, ok := params[tk]; ok {
  28. if strings.Compare(tv, v) == 0 {
  29. required = true
  30. break
  31. }
  32. }
  33. i += 2
  34. }
  35. }
  36. // Required unless all given field and its value are not equal.
  37. // Example: required-unless: id,1,age,18
  38. case "required-unless":
  39. required = true
  40. array := strings.Split(ruleVal, ",")
  41. // It supports multiple field and value pairs.
  42. if len(array)%2 == 0 {
  43. for i := 0; i < len(array); {
  44. tk := array[i]
  45. tv := array[i+1]
  46. if v, ok := params[tk]; ok {
  47. if strings.Compare(tv, v) == 0 {
  48. required = false
  49. break
  50. }
  51. }
  52. i += 2
  53. }
  54. }
  55. // Required if any of given fields are not empty.
  56. // Example: required-with:id,name
  57. case "required-with":
  58. required = false
  59. array := strings.Split(ruleVal, ",")
  60. for i := 0; i < len(array); i++ {
  61. if params[array[i]] != "" {
  62. required = true
  63. break
  64. }
  65. }
  66. // Required if all of given fields are not empty.
  67. // Example: required-with:id,name
  68. case "required-with-all":
  69. required = true
  70. array := strings.Split(ruleVal, ",")
  71. for i := 0; i < len(array); i++ {
  72. if params[array[i]] == "" {
  73. required = false
  74. break
  75. }
  76. }
  77. // Required if any of given fields are empty.
  78. // Example: required-with:id,name
  79. case "required-without":
  80. required = false
  81. array := strings.Split(ruleVal, ",")
  82. for i := 0; i < len(array); i++ {
  83. if params[array[i]] == "" {
  84. required = true
  85. break
  86. }
  87. }
  88. // Required if all of given fields are empty.
  89. // Example: required-with:id,name
  90. case "required-without-all":
  91. required = true
  92. array := strings.Split(ruleVal, ",")
  93. for i := 0; i < len(array); i++ {
  94. if params[array[i]] != "" {
  95. required = false
  96. break
  97. }
  98. }
  99. }
  100. if required {
  101. return !(value == "")
  102. } else {
  103. return true
  104. }
  105. }