func List() gforms.Form { return gforms.DefineForm(gforms.NewFields( gforms.NewTextField( "type", gforms.Validators{ gforms.Required(), gforms.MaxLengthValidator(255), }, ), gforms.NewTextField( "name", gforms.Validators{ gforms.Required(), gforms.MaxLengthValidator(255), }, ), gforms.NewTextField( "color", gforms.Validators{ gforms.Required(), gforms.MaxLengthValidator(255), gforms.RegexpValidator(`(white|black|light_pink|pink)`), }, ), )) }
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) }
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) }
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) }
func main() { tplText := `<form method="post">{{range $i, $field := .Fields}} <label>{{$field.GetName}}: </label>{{$field.Html}} {{range $ei, $err := $field.Errors}}<label class="error">{{$err}}</label>{{end}}<br /> {{end}}<input type="submit"> </form> ` tpl := template.Must(template.New("tpl").Parse(tplText)) userForm := gforms.DefineForm(gforms.NewFields( gforms.NewTextField( "name", gforms.Validators{ gforms.Required(), gforms.MaxLengthValidator(32), }, ), gforms.NewFloatField( "weight", gforms.Validators{}, ), )) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") form := userForm(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) }
func main() { tpl, _ := template.New("tpl").Parse(tplText) searchForm := gforms.DefineForm( gforms.NewFields( gforms.NewTextField( "query", gforms.Validators{ gforms.Required(), gforms.MinLengthValidator(2), }, ), )) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") form := searchForm.FromUrlValues(r.URL.Query()) if !form.IsValid() { tpl.Execute(w, form) return } fmt.Fprintf(w, "input query: %v", form.CleanedData["query"]) }) http.ListenAndServe(":9000", nil) }
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{}), ), ))