dter8514 2017-07-27 20:02
浏览 55
已采纳

遍历多个表单值,并将其中一些放入结构切片中

I'm in a situation where I want to get an unknown number of input values into my Go structs, but only some of them will be placed inside a slice/array. Here's a snippet of the HTML content:

<form action="/" method="post">
...
<tbody>
    {{ range .users }}
    <tr>
        <td class="text-center">
            <img class="avatar" src="{{ .AvatarThumbnailURL }}">
            <input type="hidden" name="thumbnail" value="{{ .AvatarThumbnailURL }}">
        </td>
        <td>
            {{ .Username }}<input type="hidden" name="username" value="{{ .Username }}">
        </td>
        <td>
            {{ .Age }}<input type="hidden" name="age" value="{{ .Age }}">
        </td>
        <td>
            {{ .Email }}<input type="hidden" name="email" value="{{ .Email }}">
        </td>
    </tr>
    <input type="hidden" name="userid" value="{{ .UserID }}">
    {{end}}
</tbody>
...
{{ .csrfField }}

// submit button

</form>

Where {{ .csrfField }} will generate a hidden input. This part is working fine.

Now the problem is how do I get this data inside a struct?

Let's say I have the following struct:

type UserFormData struct {
   UserID int
   Username string
   AvatarThumbnailURL string
   Age int
   Email string
}

But in my POST function I don't know how many users will be submitted through the form. So I guess I have to do something like this:

userFormData := make([]UserFormData, len(r.PostForm)/6) 

(divided by 6 because there are AvatarThumbnailURL, Username, Age, Email, UserID and csrfField)

Then can go through the r.PostForm like this:

i := 0
j := 0
for key, values := range r.PostForm {
    switch key {
    case "userid":
        userid, _ := strconv.Atoi(values[0])
        userFormData[j].UserID = userid
    case "thumbnail":
        userFormData[j].Thumbnail = values[0]
    // the other fields
    i++
    if i%6 == 0 {
        j++
    }
}

While this work, it's extremely error prone to indexoutofrange and whenever I make a change then I have to count number of input values that will be sent with the form and update the (currently) '6' values. I have no doubt that there must be a much, much, much better way to do this, and I would greatly appreciate if someone could point me in the right direction :)

  • 写回答

1条回答 默认 最新

  • douyu7879 2017-07-27 20:35
    关注

    You can take use the fact that r.PostForm is actually a map where keys are the field names and values are an array of strings, so you can do something along these lines (I didn't test it, but the concept should work).

    We iterate over all values for the "userid" field to know how many users there are. Also, you can use append to add users dynamically to the slice.

    userFormData := []UserFormData{}
    for i, userId := range r.PostForm["userid"] {
         user := UserFormData{
            UserID: userId,
            Thumbnail: r.PostForm["thumbnail"][i],
         }
         userFormData = append(userFormData, user)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分