示例#1
0
func main() {
	tpl, _ := template.New("tpl").Parse(tplText)
	loginForm := gforms.DefineForm(
		gforms.NewFields(
			gforms.NewTextField(
				"email",
				gforms.Validators{
					gforms.Required(),
					gforms.MinLengthValidator(4),
					gforms.EmailValidator(),
				},
			),
			gforms.NewTextField(
				"password",
				gforms.Validators{
					gforms.Required(),
					gforms.MinLengthValidator(4),
					gforms.MaxLengthValidator(16),
				},
				gforms.PasswordInputWidget(map[string]string{}),
			),
		))
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		form := loginForm(r)
		if r.Method != "POST" {
			tpl.Execute(w, form)
			return
		}
		if !form.IsValid() {
			tpl.Execute(w, form)
			return
		}
		user := User{}
		form.MapTo(&user)
		fmt.Fprintf(w, "ok: %v", user)
	})
	http.ListenAndServe(":9000", nil)
}
示例#2
0
package forms

import (
	"github.com/bluele/gforms"
)

var authentication = gforms.DefineForm(
	gforms.NewFields(
		gforms.NewTextField(
			"email",
			gforms.Validators{
				gforms.Required(),
				gforms.MinLengthValidator(4),
				gforms.EmailValidator(),
			},
		),
		gforms.NewTextField(
			"password",
			gforms.Validators{
				gforms.Required(),
				gforms.MinLengthValidator(4),
				gforms.MaxLengthValidator(16),
			},
			gforms.PasswordInputWidget(map[string]string{}),
		),
	))