You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fmt.Println(err.Error()) // output: Field validation for "Age" failed on the "lte" tag
92
+
fmt.Println(err.Field) // output: Age
93
+
fmt.Println(err.Tag) // output: lte
94
+
fmt.Println(err.Kind) // output: uint8
95
+
fmt.Println(err.Type) // output: uint8
96
+
fmt.Println(err.Param) // output: 130
97
+
fmt.Println(err.Value) // output: 135
98
+
99
+
// or if you prefer you can use the Flatten function
100
+
// NOTE: I find this usefull when using a more hard static approach of checking field errors.
101
+
// The above, is best for passing to some generic code to say parse the errors. i.e. I pass errs
102
+
// to a routine which loops through the errors, creates and translates the error message into the
103
+
// users locale and returns a map of map[string]string // field and error which I then use
104
+
// within the HTML rendering.
105
+
106
+
flat:= errs.Flatten()
107
+
fmt.Println(flat) // output: map[Age:Field validation for "Age" failed on the "lte" tag Addresses[0].Address.City:Field validation for "City" failed on the "required" tag]
108
+
err = flat["Addresses[0].Address.City"]
109
+
fmt.Println(err.Field) // output: City
110
+
fmt.Println(err.Tag) // output: required
111
+
fmt.Println(err.Kind) // output: string
112
+
fmt.Println(err.Type) // output: string
113
+
fmt.Println(err.Param) // output:
114
+
fmt.Println(err.Value) // output:
115
+
116
+
// from here you can create your own error messages in whatever language you wish
117
+
return
118
+
}
119
+
120
+
// save user to database
121
+
}
122
+
```
123
+
124
+
Benchmarks
125
+
------
126
+
###### Run on MacBook Pro (Retina, 15-inch, Late 2013) 2.6 GHz Intel Core i7 16 GB 1600 MHz DDR3
fmt.Println(err.Error()) // output: Field validation for "Age" failed on the "lte" tag
56
+
fmt.Println(err.Field) // output: Age
57
+
fmt.Println(err.Tag) // output: lte
58
+
fmt.Println(err.Kind) // output: uint8
59
+
fmt.Println(err.Type) // output: uint8
60
+
fmt.Println(err.Param) // output: 130
61
+
fmt.Println(err.Value) // output: 135
62
+
63
+
// or if you prefer you can use the Flatten function
64
+
// NOTE: I find this usefull when using a more hard static approach of checking field errors.
65
+
// The above, is best for passing to some generic code to say parse the errors. i.e. I pass errs
66
+
// to a routine which loops through the errors, creates and translates the error message into the
67
+
// users locale and returns a map of map[string]string // field and error which I then use
68
+
// within the HTML rendering.
69
+
70
+
flat:=errs.Flatten()
71
+
fmt.Println(flat) // output: map[Age:Field validation for "Age" failed on the "lte" tag Addresses[0].Address.City:Field validation for "City" failed on the "required" tag]
72
+
err=flat["Addresses[0].Address.City"]
73
+
fmt.Println(err.Field) // output: City
74
+
fmt.Println(err.Tag) // output: required
75
+
fmt.Println(err.Kind) // output: string
76
+
fmt.Println(err.Type) // output: string
77
+
fmt.Println(err.Param) // output:
78
+
fmt.Println(err.Value) // output:
79
+
80
+
// from here you can create your own error messages in whatever language you wish
0 commit comments