如何处理使用 Revel Golang 的 HTML 表单中的动态数量的输入?

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

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:

&lt;form action=&quot;{{url &quot;Votes.CreateVote&quot;}}&quot; id=&quot;formVoteCreate&quot; method=&quot;POST&quot;&gt;
			&lt;p class=&quot;field&quot;&gt;
				&lt;label&gt;Question:&lt;/label&gt;
				&lt;input type=&quot;text&quot; name=&quot;question&quot; size=&quot;40&quot;  /&gt;
			&lt;/p&gt;
			 &lt;div ng-repeat=&quot;answer in answers&quot;&gt;
			 	&lt;p class=&quot;field&quot;&gt;
					&lt;label&gt;//stuff.title//:&lt;/label&gt;
					&lt;input type=&quot;text&quot; name=//stuff.name// size=&quot;40&quot;  /&gt;
				&lt;/p&gt;
			 &lt;/div&gt;
			&lt;p&gt;
				&lt;input ng-click=&quot;addInput()&quot; class=&quot;btn&quot;  type=&quot;button&quot; value=&quot;Add answer&quot;&gt;
			&lt;/p&gt;
			&lt;p class=&quot;buttons&quot;&gt;
				&lt;input class=&quot;btn&quot; type=&quot;submit&quot; value=&quot;Create&quot; /&gt;
			&lt;/p&gt;
		&lt;/form&gt;

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&amp;text=value2&amp;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(&quot;inputname&quot;) method, as this would only give the first result. Instead, we need to access the values in the Form map directly:

package controllers

import (
	&quot;log&quot;

	&quot;github.com/robfig/revel&quot;
)

type App struct {
	*revel.Controller
}

func (c App) Index() revel.Result {
	if err := c.Request.ParseForm(); err != nil {
		// handle error
	}
	values := c.Request.Form[&quot;text&quot;]
	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&amp;text=value2&amp;text=value3, as would happen for a form with method=&quot;GET&quot; and three text inputs with name=&quot;text&quot;:

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.

huangapple
  • 本文由 发表于 2016年3月7日 19:57:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/35843156.html
匿名

发表评论

匿名网友

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

确定