summaryrefslogtreecommitdiff
path: root/system/admin/handlers.go
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2017-01-20 19:02:48 -0800
committerSteve Manuel <nilslice@gmail.com>2017-01-20 19:02:48 -0800
commit9bead643624d3052e331b3f0baf379d62846b96a (patch)
tree3de63440f50c05adeb226180da3fa60a0b50ba0b /system/admin/handlers.go
parent13e3226f8095d27c4d70756560a7575837d80495 (diff)
adding fix for consistent order in repeaters and multi-value field inputs
Diffstat (limited to 'system/admin/handlers.go')
-rw-r--r--system/admin/handlers.go46
1 files changed, 36 insertions, 10 deletions
diff --git a/system/admin/handlers.go b/system/admin/handlers.go
index 2bea356..89eda6a 100644
--- a/system/admin/handlers.go
+++ b/system/admin/handlers.go
@@ -1561,23 +1561,49 @@ func editHandler(res http.ResponseWriter, req *http.Request) {
// check for any multi-value fields (ex. checkbox fields)
// and correctly format for db storage. Essentially, we need
// fieldX.0: value1, fieldX.1: value2 => fieldX: []string{value1, value2}
- var discardKeys []string
+ fieldOrderValue := make(map[string]map[string][]string)
+ ordVal := make(map[string][]string)
for k, v := range req.PostForm {
if strings.Contains(k, ".") {
- key := strings.Split(k, ".")[0]
+ fo := strings.Split(k, ".")
- if req.PostForm.Get(key) == "" {
- req.PostForm.Set(key, v[0])
- } else {
- req.PostForm.Add(key, v[0])
- }
+ // put the order and the field value into map
+ field := string(fo[0])
+ order := string(fo[1])
+ fieldOrderValue[field] = ordVal
+
+ // orderValue is 0:[?type=Thing&id=1]
+ orderValue := fieldOrderValue[field]
+ orderValue[order] = v
+ fieldOrderValue[field] = orderValue
- discardKeys = append(discardKeys, k)
+ // discard the post form value with name.N
+ req.PostForm.Del(k)
}
+
}
- for _, discardKey := range discardKeys {
- req.PostForm.Del(discardKey)
+ // add/set the key & value to the post form in order
+ for f, ov := range fieldOrderValue {
+ for i := 0; i < len(ov); i++ {
+ position := fmt.Sprintf("%d", i)
+ fieldValue := ov[position]
+ fmt.Println(position, fieldValue)
+
+ if req.PostForm.Get(f) == "" {
+ for i, fv := range fieldValue {
+ if i == 0 {
+ req.PostForm.Set(f, fv)
+ } else {
+ req.PostForm.Add(f, fv)
+ }
+ }
+ } else {
+ for _, fv := range fieldValue {
+ req.PostForm.Add(f, fv)
+ }
+ }
+ }
}
pt := t