下面結合之前創建的myapp做一個提交表單的demo
首先編輯app/views/Application/Index.html 模板文件添加一下form表單
<form action="/Application/Hello" method="GET">
<input type="text" name="myName" />
<input type="submit" value="Say hello!" />
</form>
刷新表單

我們提交一下表單

報錯:沒有找到匹配的action,下面我們來添加一個
func (c Application) Hello(myName string) rev.Result {
return c.Render(myName)
}
接着我們創建視圖,路徑為:app/views/Application/Hello.html ,內容如下:
{{set . "title" "Home"}}
{{template "header.html" .}}
<h1>Hello {{.myName}}</h1>
<a href="/">Back to form</a>
{{template "footer.html" .}}
在文本框中填入內容然后提交表單

最后我們添加一些驗證,把文本框中的內容要求為必填並且最少三個字符,我們來編輯你的action app/controllers/app.go 文件
func (c Application) Hello(myName string) rev.Result {
c.Validation.Required(myName).Message("Your name is required!")
c.Validation.MinSize(myName, 3).Message("Your name is not long enough!")
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(Application.Index)
}
return c.Render(myName)
}
修改app/views/Application/Index.html模板文件
<h1>Aloha World</h1>
{{range .errors}}
<p style="color:#c00">
{{.Message}}
</p>
{{end}}
<form action="/Application/Hello" method="GET">
<input type="text" name="myName" value="{{.flash.myName}}" />
<input type="submit" value="Say hello!" />
</form>
現在回到Index頁面,如果填寫內容不符合要求將提示錯誤信息如下所示:

至此結束。
