Skip to content

Commit 487533f

Browse files
committed
Fix validate() function to handle errors in embedded anon structs
1 parent 4cb1bdd commit 487533f

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
@@ -310,6 +310,10 @@ func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaro
310310
}
311311

312312
data["HasError"] = true
313+
// If the field with name errs[0].FieldNames[0] is not found in form
314+
// somehow, some code later on will panic on Data["ErrorMsg"].(string).
315+
// So initialize it to some default.
316+
data["ErrorMsg"] = l.Tr("form.unknown_error")
313317
AssignForm(f, data)
314318

315319
typ := reflect.TypeOf(f)
@@ -320,16 +324,9 @@ func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaro
320324
val = val.Elem()
321325
}
322326

323-
for i := 0; i < typ.NumField(); i++ {
324-
field := typ.Field(i)
325-
327+
if field, ok := typ.FieldByName(errs[0].FieldNames[0]); ok {
326328
fieldName := field.Tag.Get("form")
327-
// Allow ignored fields in the struct
328-
if fieldName == "-" {
329-
continue
330-
}
331-
332-
if errs[0].FieldNames[0] == field.Name {
329+
if fieldName != "-" {
333330
data["Err_"+field.Name] = true
334331

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

0 commit comments

Comments
 (0)