support default value for form

This commit is contained in:
thinkerou 2017-10-24 17:44:09 +08:00
parent dfb68ce085
commit 13042dfd4e

View File

@ -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)