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 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题