Skip to content

Commit

Permalink
Merge pull request #170 from torniker/ponzu-dev
Browse files Browse the repository at this point in the history
fixed approve handler losing data for string slices
  • Loading branch information
nilslice authored Jun 26, 2017
2 parents fb56605 + abd0e59 commit cd6be5e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions system/db/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd6be5e

Please sign in to comment.