Skip to content

Commit aead030

Browse files
committed
Fix validate() function to handle errors in embedded anon structs
1 parent b16be15 commit aead030

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

modules/auth/auth.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaro
305305
}
306306

307307
data["HasError"] = true
308+
// If the field with name errs[0].FieldNames[0] is not found in form
309+
// somehow, some code later on will panic on Data["ErrorMsg"].(string).
310+
// So initialize it to some default.
311+
data["ErrorMsg"] = l.Tr("form.unknown_error")
308312
AssignForm(f, data)
309313

310314
typ := reflect.TypeOf(f)
@@ -315,16 +319,9 @@ func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaro
315319
val = val.Elem()
316320
}
317321

318-
for i := 0; i < typ.NumField(); i++ {
319-
field := typ.Field(i)
320-
322+
if field, ok := typ.FieldByName(errs[0].FieldNames[0]); ok {
321323
fieldName := field.Tag.Get("form")
322-
// Allow ignored fields in the struct
323-
if fieldName == "-" {
324-
continue
325-
}
326-
327-
if errs[0].FieldNames[0] == field.Name {
324+
if fieldName != "-" {
328325
data["Err_"+field.Name] = true
329326

330327
trName := field.Tag.Get("locale")

0 commit comments

Comments
 (0)