diff --git a/binding/form_mapping.go b/binding/form_mapping.go index c968dc08..e824d39c 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -8,6 +8,7 @@ import ( "errors" "reflect" "strconv" + "strings" "time" ) @@ -23,6 +24,15 @@ func mapForm(ptr interface{}, form map[string][]string) error { structFieldKind := structField.Kind() inputFieldName := typeField.Tag.Get("form") + inputFieldNameList := strings.Split(inputFieldName, ",") + inputFieldName = inputFieldNameList[0] + var defaultValue interface{} + if len(inputFieldNameList) > 1 { + defaultList := strings.Split(inputFieldNameList[1], "=") + if len(defaultList) == 2 && defaultList[0] == "default" { + defaultValue = defaultList[1] + } + } if inputFieldName == "" { inputFieldName = typeField.Name @@ -38,8 +48,13 @@ func mapForm(ptr interface{}, form map[string][]string) error { } } inputValue, exists := form[inputFieldName] + if !exists { - continue + if defaultValue.(string) == "" { + continue + } + inputValue = make([]string, 1) + inputValue[0] = defaultValue.(string) } numElems := len(inputValue)