diff options
Diffstat (limited to 'system/db/content.go')
-rw-r--r-- | system/db/content.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/system/db/content.go b/system/db/content.go index 940c57b..e73d803 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -711,6 +711,53 @@ func postToJSON(ns string, data url.Values) ([]byte, error) { } post := t() + // 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} + fieldOrderValue := make(map[string]map[string][]string) + for k, v := range data { + if strings.Contains(k, ".") { + fo := strings.Split(k, ".") + + // put the order and the field value into map + field := string(fo[0]) + order := string(fo[1]) + if len(fieldOrderValue[field]) == 0 { + fieldOrderValue[field] = make(map[string][]string) + } + + // orderValue is 0:[?type=Thing&id=1] + orderValue := fieldOrderValue[field] + orderValue[order] = v + fieldOrderValue[field] = orderValue + + // discard the post form value with name.N + data.Del(k) + } + } + + // 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] + + if data.Get(f) == "" { + for i, fv := range fieldValue { + if i == 0 { + data.Set(f, fv) + } else { + data.Add(f, fv) + } + } + } else { + for _, fv := range fieldValue { + data.Add(f, fv) + } + } + } + } + dec := schema.NewDecoder() dec.SetAliasTag("json") // allows simpler struct tagging when creating a content type dec.IgnoreUnknownKeys(true) // will skip over form values submitted, but not in struct |