@@ -28,6 +28,8 @@ func (e *ValidationError) String() string {
28
28
// Validation context manages data validation and error messages.
29
29
type Validation struct {
30
30
Errors []* ValidationError
31
+ Request * Request
32
+ Translator func (locale , message string , args ... interface {}) string
31
33
keep bool
32
34
}
33
35
@@ -66,19 +68,31 @@ func (v *Validation) ErrorMap() map[string]*ValidationError {
66
68
67
69
// Error adds an error to the validation context.
68
70
func (v * Validation ) Error (message string , args ... interface {}) * ValidationResult {
69
- result := (& ValidationResult {
70
- Ok : false ,
71
- Error : & ValidationError {},
72
- }).Message (message , args ... )
71
+ result := v .ValidationResult (false ).Message (message ,args ... )
73
72
v .Errors = append (v .Errors , result .Error )
74
73
return result
75
74
}
76
-
75
+ // Error adds an error to the validation context.
76
+ func (v * Validation ) ErrorKey (message string , args ... interface {}) * ValidationResult {
77
+ result := v .ValidationResult (false ).MessageKey (message ,args ... )
78
+ v .Errors = append (v .Errors , result .Error )
79
+ return result
80
+ }
81
+ // Error adds an error to the validation context.
82
+ func (v * Validation ) ValidationResult (ok bool ) * ValidationResult {
83
+ if ok {
84
+ return & ValidationResult {Ok :ok }
85
+ } else {
86
+ return & ValidationResult {Ok :ok , Error : & ValidationError {}, Locale :v .Request .Locale , Translator :v .Translator }
87
+ }
88
+ }
77
89
// ValidationResult is returned from every validation method.
78
90
// It provides an indication of success, and a pointer to the Error (if any).
79
91
type ValidationResult struct {
80
92
Error * ValidationError
81
93
Ok bool
94
+ Locale string
95
+ Translator func (locale , message string , args ... interface {}) string
82
96
}
83
97
84
98
// Key sets the ValidationResult's Error "key" and returns itself for chaining
@@ -102,6 +116,23 @@ func (r *ValidationResult) Message(message string, args ...interface{}) *Validat
102
116
return r
103
117
}
104
118
119
+ // Allow a message key to be passed into the validation result. The Validation has already
120
+ // setup the translator to translate the message key
121
+ func (r * ValidationResult ) MessageKey (message string , args ... interface {}) * ValidationResult {
122
+ if r .Error == nil {
123
+ return r
124
+ }
125
+
126
+ // If translator found, use that to create the message, otherwise call Message method
127
+ if r .Translator != nil {
128
+ r .Error .Message = r .Translator (r .Locale , message , args ... )
129
+ } else {
130
+ r .Message (message , args ... )
131
+ }
132
+
133
+ return r
134
+ }
135
+
105
136
// Required tests that the argument is non-nil and non-empty (if string or list)
106
137
func (v * Validation ) Required (obj interface {}) * ValidationResult {
107
138
return v .apply (Required {}, obj )
@@ -177,7 +208,7 @@ func (v *Validation) FilePath(str string, m int) *ValidationResult {
177
208
178
209
func (v * Validation ) apply (chk Validator , obj interface {}) * ValidationResult {
179
210
if chk .IsSatisfied (obj ) {
180
- return & ValidationResult { Ok : true }
211
+ return v . ValidationResult ( true )
181
212
}
182
213
183
214
// Get the default key.
@@ -199,10 +230,9 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
199
230
v .Errors = append (v .Errors , err )
200
231
201
232
// Also return it in the result.
202
- return & ValidationResult {
203
- Ok : false ,
204
- Error : err ,
205
- }
233
+ vr := v .ValidationResult (false )
234
+ vr .Error = err
235
+ return vr
206
236
}
207
237
208
238
// Check applies a group of validators to a field, in order, and return the
@@ -224,13 +254,15 @@ func ValidationFilter(c *Controller, fc []Filter) {
224
254
// If json request, we shall assume json response is intended,
225
255
// as such no validation cookies should be tied response
226
256
if c .Params != nil && c .Params .JSON != nil {
227
- c .Validation = & Validation {}
257
+ c .Validation = & Validation {Request : c . Request , Translator : MessageFunc }
228
258
fc [0 ](c , fc [1 :])
229
259
} else {
230
260
errors , err := restoreValidationErrors (c .Request )
231
261
c .Validation = & Validation {
232
262
Errors : errors ,
233
263
keep : false ,
264
+ Request :c .Request ,
265
+ Translator :MessageFunc ,
234
266
}
235
267
hasCookie := (err != http .ErrNoCookie )
236
268
0 commit comments