Exemplo n.º 1
0
func main() {
	Form := gforms.DefineModelForm(User{}, gforms.NewFields(
		gforms.NewTextField(
			"name",
			gforms.Validators{
				gforms.Required(),
			},
		),
	))
	http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) {
		form := Form(r)
		if r.Method != "POST" {
			fmt.Fprintf(w, form.Html())
			return
		}
		if form.IsValid() {
			user := form.GetModel().(User)
			fmt.Fprintf(w, "%v => %v", form.CleanedData, user)
		} else {
			fmt.Println(form.Html())
			fmt.Fprintf(w, "%v", form.Errors())
		}
	})
	http.ListenAndServe(":9000", nil)
}
Exemplo n.º 2
0
func main() {
	tpl := template.Must(template.ParseFiles(path.Join(getTemplatePath(), "post_form.html")))
	langForm := gforms.DefineModelForm(Lang{}, gforms.NewFields(
		gforms.NewTextField(
			"name",
			gforms.Validators{
				gforms.Required(),
				CustomValidator{
					Langs: []string{"golang", "python", "c"},
				},
			},
		),
	))

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		form := langForm(r)
		if r.Method != "POST" {
			tpl.Execute(w, form)
			return
		}
		if !form.IsValid() {
			tpl.Execute(w, form)
			return
		}
		lang := form.GetModel().(Lang)
		fmt.Fprintf(w, "ok: %v", lang)
	})
	http.ListenAndServe(":9000", nil)
}