@@ -28,6 +28,8 @@ func (e *ValidationError) String() string {
2828// Validation context manages data validation and error messages.
2929type Validation struct {
3030 Errors []* ValidationError
31+ Request * Request
32+ Translator func (locale , message string , args ... interface {}) string
3133 keep bool
3234}
3335
@@ -66,19 +68,31 @@ func (v *Validation) ErrorMap() map[string]*ValidationError {
6668
6769// Error adds an error to the validation context.
6870func (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 ... )
7372 v .Errors = append (v .Errors , result .Error )
7473 return result
7574}
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+ }
7789// ValidationResult is returned from every validation method.
7890// It provides an indication of success, and a pointer to the Error (if any).
7991type ValidationResult struct {
8092 Error * ValidationError
8193 Ok bool
94+ Locale string
95+ Translator func (locale , message string , args ... interface {}) string
8296}
8397
8498// 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
102116 return r
103117}
104118
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+
105136// Required tests that the argument is non-nil and non-empty (if string or list)
106137func (v * Validation ) Required (obj interface {}) * ValidationResult {
107138 return v .apply (Required {}, obj )
@@ -177,7 +208,7 @@ func (v *Validation) FilePath(str string, m int) *ValidationResult {
177208
178209func (v * Validation ) apply (chk Validator , obj interface {}) * ValidationResult {
179210 if chk .IsSatisfied (obj ) {
180- return & ValidationResult { Ok : true }
211+ return v . ValidationResult ( true )
181212 }
182213
183214 // Get the default key.
@@ -199,10 +230,9 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
199230 v .Errors = append (v .Errors , err )
200231
201232 // 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
206236}
207237
208238// Check applies a group of validators to a field, in order, and return the
@@ -224,13 +254,15 @@ func ValidationFilter(c *Controller, fc []Filter) {
224254 // If json request, we shall assume json response is intended,
225255 // as such no validation cookies should be tied response
226256 if c .Params != nil && c .Params .JSON != nil {
227- c .Validation = & Validation {}
257+ c .Validation = & Validation {Request : c . Request , Translator : MessageFunc }
228258 fc [0 ](c , fc [1 :])
229259 } else {
230260 errors , err := restoreValidationErrors (c .Request )
231261 c .Validation = & Validation {
232262 Errors : errors ,
233263 keep : false ,
264+ Request :c .Request ,
265+ Translator :MessageFunc ,
234266 }
235267 hasCookie := (err != http .ErrNoCookie )
236268
0 commit comments