英文:
How to handle dynamic number of inputs from html form with with Revel Golang
问题
我有一个带有动态数量输入的HTML表单。每个输入都必须是一个Model对象,我还有一个函数,用于接收这些输入的值。
我的HTML表单:
<form action="{{url "Votes.CreateVote"}}" id="formVoteCreate" method="POST">
<p class="field">
<label>Question:</label>
<input type="text" name="question" size="40" />
</p>
<div ng-repeat="answer in answers">
<p class="field">
<label>//stuff.title//:</label>
<input type="text" name=//stuff.name// size="40" />
</p>
</div>
<p>
<input ng-click="addInput()" class="btn" type="button" value="Add answer">
</p>
<p class="buttons">
<input class="btn" type="submit" value="Create" />
</p>
</form>
以及Revel框架的Go语言处理程序:
func (c Votes) CreateVote() revel.Result {
// 在这个位置,我想从HTML表单中获取一个包含答案的切片
return c.Redirect(routes.App.Index())
}
还有答案模型:
type Answer struct {
Model
Text string
}
我该如何将表单的值作为一个包含答案的切片发送,并打包到Model中?
英文:
I have an html form with dynamic number of inputs. Each input must be a Model object, and i also have a function, which receives values from this inputs.
My html form:
<form action="{{url "Votes.CreateVote"}}" id="formVoteCreate" method="POST">
<p class="field">
<label>Question:</label>
<input type="text" name="question" size="40" />
</p>
<div ng-repeat="answer in answers">
<p class="field">
<label>//stuff.title//:</label>
<input type="text" name=//stuff.name// size="40" />
</p>
</div>
<p>
<input ng-click="addInput()" class="btn" type="button" value="Add answer">
</p>
<p class="buttons">
<input class="btn" type="submit" value="Create" />
</p>
</form>
And revel golang handler:
func (c Votes) CreateVote() revel.Result {
// in this place i want get a slice with answers from html form
return c.Redirect(routes.App.Index())
}
and answer model:
type Answer struct {
Model
Text string
}
How can i send form's values as a slice with answers packed to Model?
答案1
得分: 1
每个Revel控制器都附带一个附加的Request
,它实际上只是一个普通的Go标准库Request
。因此,与普通的Go Web服务器一样,我们不能使用Request.Form.Get("inputname")
方法,因为这只会给出第一个结果。相反,我们需要直接访问Form
映射中的值:
package controllers
import (
"log"
"github.com/robfig/revel"
)
type App struct {
*revel.Controller
}
func (c App) Index() revel.Result {
if err := c.Request.ParseForm(); err != nil {
// 处理错误
}
values := c.Request.Form["text"]
for i := range values {
log.Println(values[i])
}
return c.Render()
}
上面的示例是一个简单的应用程序,就像在启动项目时Revel生成的应用程序一样,其中有一个名为text
的输入,并且可以根据您的特定情况进行调整。
values
变量的类型是[]string
,因此,如果您提交一个带有查询字符串?text=value1&text=value2&text=value3
的GET请求,就像使用method="GET"
和三个name="text"
的文本输入的表单一样,将会记录如下内容:
2016/03/07 22:16:41 app.go:19: value1
2016/03/07 22:16:41 app.go:19: value2
2016/03/07 22:16:41 app.go:19: value3
问题中的表单恰好使用POST方法,但从表单中读取值的代码保持不变。
英文:
Each Revel controller comes with an attached Request
, which is really just a normal Go standard library Request
. As such, this is the same as for plain Go web servers, in that we cannot use the Request.Form.Get("inputname")
method, as this would only give the first result. Instead, we need to access the values in the Form
map directly:
package controllers
import (
"log"
"github.com/robfig/revel"
)
type App struct {
*revel.Controller
}
func (c App) Index() revel.Result {
if err := c.Request.ParseForm(); err != nil {
// handle error
}
values := c.Request.Form["text"]
for i := range values {
log.Println(values[i])
}
return c.Render()
}
The example above is for a simple application like the one generated by Revel when you start a project, with an input named text
, and can be adapted to your specific case.
The values
variable is of type []string
, which is why the following is logged, if you submit a GET request with query string ?text=value1&text=value2&text=value3
, as would happen for a form with method="GET"
and three text inputs with name="text"
:
2016/03/07 22:16:41 app.go:19: value1
2016/03/07 22:16:41 app.go:19: value2
2016/03/07 22:16:41 app.go:19: value3
The form in the question happens to use method POST, but the code for reading the values from the form remains the same.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论