How to parse form array in golang Beego

huangapple go评论92阅读模式
英文:

How to parse form array in golang Beego

问题

如何使用Beego解析HTML表单数组。

<input name="names[]" type="text" />
<input name="names[]" type="text" />
<input name="names[]" type="text" />

Go Beego

type Rsvp struct {
    Id    int      `form:"-"`
    Names []string `form:"names[]"`
}

rsvp := Rsvp{}
if err := this.ParseForm(&rsvp); err != nil {
    //处理错误
}

input := this.Input()
fmt.Printf("%+v\n", input) // map[names[]:[name1 name2 name3]]
fmt.Printf("%+v\n", rsvp) // {Names:[]}

为什么Beego的ParseForm方法返回一个空的names?

如何将值获取到rsvp.Names中?

英文:

How to parse html form array with Beego.

<!-- language-all: lang-html -->

&lt;input name=&quot;names[]&quot; type=&quot;text&quot; /&gt;
&lt;input name=&quot;names[]&quot; type=&quot;text&quot; /&gt;```


Go Beego


    type Rsvp struct {
        Id    int      `form:&quot;-&quot;`
        Names []string `form:&quot;names[]&quot;`
    }

    rsvp := Rsvp{}
    if err := this.ParseForm(&amp;rsvp); err != nil {
        //handle error
    }

    input := this.Input()
    fmt.Printf(&quot;%+v\n&quot;, input) // map[names[]:[name1 name2 name3]]
    fmt.Printf(&quot;%+v\n&quot;, rsvp) // {Names:[]}


Why Beego ParseForm method return an empty names?

How to get values into rsvp.Names?

</details>


# 答案1
**得分**: 6

感谢 @ysqi 给了我一个提示。我正在添加一个更详细的示例来解析类似于beego中的关联数组形式的表单数据。

这是我的表单结构:

    <input name="contacts[0][email]" type="text" value="a1@gmail.com"/>
    <input name="contacts[0][first_name]" type="text" value="f1"/>
    <input name="contacts[0][last_name]" type="text" value="l1"/>
    <input name="contacts[1][email]" type="text" value="a2@gmail.com"/>
    <input name="contacts[1][first_name]" type="text" value="f2"/>
    <input name="contacts[1][last_name]" type="text" value="l2"/>

golang(beego) 代码:

    contacts := make([]map[string]string, 0, 3)
    
    this.Ctx.Input.Bind(&contacts, "contacts")

contacts 变量:

    [
      {
        "email": "user2@gmail.com",
        "first_name": "Sam",
        "last_name": "Gamge"
      },
      {
        "email": "user3@gmail.com",
        "first_name": "john",
        "last_name": "doe"
      }
    ]

现在你可以像这样使用它:

    for _, contact := range contacts {
    	contact["email"]
    	contact["first_name"]
    	contact["last_name"]
    }

<details>
<summary>英文:</summary>

Thanks @ysqi for giving me a hint. I am adding a little detailed example to parse associate array like form data in beego

Here is my form structure:

    &lt;input name=&quot;contacts[0][email]&quot; type=&quot;text&quot; value=&quot;a1@gmail.com&quot;/&gt;
    &lt;input name=&quot;contacts[0][first_name]&quot; type=&quot;text&quot; value=&quot;f1&quot;/&gt;
    &lt;input name=&quot;contacts[0][last_name]&quot; type=&quot;text&quot; value=&quot;l1&quot;/&gt;
    &lt;input name=&quot;contacts[1][email]&quot; type=&quot;text&quot; value=&quot;a2@gmail.com&quot;/&gt;
    &lt;input name=&quot;contacts[1][first_name]&quot; type=&quot;text&quot; value=&quot;f2&quot;/&gt;
    &lt;input name=&quot;contacts[1][last_name]&quot; type=&quot;text&quot; value=&quot;l2&quot;/&gt;

golang(beego) code:

    contacts := make([]map[string]string, 0, 3)
    
    this.Ctx.Input.Bind(&amp;contacts, &quot;contacts&quot;)

contacts variable:

    [
      {
        &quot;email&quot;: &quot;user2@gmail.com&quot;,
        &quot;first_name&quot;: &quot;Sam&quot;,
        &quot;last_name&quot;: &quot;Gamge&quot;
      },
      {
        &quot;email&quot;: &quot;user3@gmail.com&quot;,
        &quot;first_name&quot;: &quot;john&quot;,
        &quot;last_name&quot;: &quot;doe&quot;
      }
    ]

Now you can use it like:

    for _, contact := range contacts {
    	contact[&quot;email&quot;]
    	contact[&quot;first_name&quot;]
    	contact[&quot;last_name&quot;]
    }

</details>



# 答案2
**得分**: 4

从Request的FormValue方法的实现中可以看出,如果有多个值,它会返回第一个值:
http://golang.org/src/pkg/net/http/request.go?s=23078:23124#L795
最好的做法是直接获取属性r.Form[key],然后手动遍历所有的结果。我不确定Beego是如何工作的,但是只使用原始的Request.ParseForm和Request.Form或Request.PostForm映射应该可以完成任务。
http://golang.org/src/pkg/net/http/request.go?s=1939:6442#L61

<details>
<summary>英文:</summary>

As you can see from the implementation of the FormValue method of the Request, it returns the first value in case of multiple ones:
http://golang.org/src/pkg/net/http/request.go?s=23078:23124#L795
It would be better to get the attribute itself r.Form[key] and iterate over all the results manually. I am not sure how Beego works, but just using the raw 
Request.ParseForm and Request.Form or Request.PostForm maps should do the job.
http://golang.org/src/pkg/net/http/request.go?s=1939:6442#L61 

</details>



# 答案3
**得分**: 2

你可以这样做:参见[文档](http://beego.me/docs/mvc/controller/params.md)

    v := make([]string, 0, 3)
    this.Ctx.Input.Bind(&v, "names")

<details>
<summary>英文:</summary>

You can do like this : see [doc](http://beego.me/docs/mvc/controller/params.md)

    v := make([]string, 0, 3)
	this.Ctx.Input.Bind(&amp;v, &quot;names&quot;)

</details>



huangapple
  • 本文由 发表于 2014年5月18日 00:47:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/23713817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定