mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 18:22:23 +08:00
support default value for form
This commit is contained in:
parent
dfb68ce085
commit
13042dfd4e
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user