-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support default value for form #1138
Changes from 1 commit
13042df
b4d1b55
4654192
e9773bf
8027a23
8a14fbd
bcd046b
5a1c743
ecea8b5
0d2480e
a41721a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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], "=") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we should use |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if inputValue is empty array(inputField exist and its ""), default value not take effect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you post one example? thanks! |
||
continue | ||
if defaultValue.(string) == "" { | ||
continue | ||
} | ||
inputValue = make([]string, 1) | ||
inputValue[0] = defaultValue.(string) | ||
} | ||
|
||
numElems := len(inputValue) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any need to cast it as
interface{}
? As I see below, you'll always use it as string and parse it to the specific type, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, use
string
, thanks!