本文介绍了如何解析golang Beego中的表单数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用Beego解析html表单数组。


< input name =names []type =文字/>
< input name =names []type =text/>
< input name =names []type =text/>



Go Beego

type Rsvp struct {
Id int`form: - `
Names [ ]字符串`form:names []`
}

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

输入:= this.Input()
fmt.Printf(%+ v \ n ,输入)// map [names []:[name1 name2 name3]]
fmt.Printf(%+ v\\\
,rsvp)// {名称:[]}

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

如何获取值转换为rsvp.Names?

解决方案

正如您从Request的FormValue方法的实现中可以看到的那样,它返回第一个多重价值:

获取属性本身会更好r.Form [key]并手动迭代所有结果。我不确定Beego是如何工作的,但只使用原始的
Request.ParseForm和Request.Form或Request.PostForm地图可以完成这项工作。


How to parse html form array with Beego.

<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 {
    //handle error
}

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

Why Beego ParseForm method return an empty names?

How to get values into rsvp.Names?

解决方案

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#L795It 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

这篇关于如何解析golang Beego中的表单数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:38