mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +08:00
support default value for form
This commit is contained in:
parent
dfb68ce085
commit
13042dfd4e
@ -8,6 +8,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,6 +24,15 @@ func mapForm(ptr interface{}, form map[string][]string) error {
|
|||||||
|
|
||||||
structFieldKind := structField.Kind()
|
structFieldKind := structField.Kind()
|
||||||
inputFieldName := typeField.Tag.Get("form")
|
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 == "" {
|
if inputFieldName == "" {
|
||||||
inputFieldName = typeField.Name
|
inputFieldName = typeField.Name
|
||||||
|
|
||||||
@ -38,8 +48,13 @@ func mapForm(ptr interface{}, form map[string][]string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
inputValue, exists := form[inputFieldName]
|
inputValue, exists := form[inputFieldName]
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
continue
|
if defaultValue.(string) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
inputValue = make([]string, 1)
|
||||||
|
inputValue[0] = defaultValue.(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
numElems := len(inputValue)
|
numElems := len(inputValue)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user