Skip to content

Commit

Permalink
Fix validate() function to handle errors in embedded anon structs
Browse files Browse the repository at this point in the history
  • Loading branch information
WGH- committed Aug 8, 2019
1 parent b16be15 commit aead030
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions modules/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaro
}

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

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

for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)

if field, ok := typ.FieldByName(errs[0].FieldNames[0]); ok {
fieldName := field.Tag.Get("form")
// Allow ignored fields in the struct
if fieldName == "-" {
continue
}

if errs[0].FieldNames[0] == field.Name {
if fieldName != "-" {
data["Err_"+field.Name] = true

trName := field.Tag.Get("locale")
Expand Down

0 comments on commit aead030

Please sign in to comment.