goform
goform copied to clipboard
Golang web forms, html5 support, file upload, bind from interface, bind from request, map to struct, template support, validations, filters and build query support
GoForm (GoLang Web Forms)
Golang Web Forms validations, rendering and binding.
Features
- HTML5 Support
- Bind From Interface
- Bind From Request
- Map to your struct
- Validation
- Build Query String
- Template
Installation
go get github.com/semihs/goform
Examples
See examples.
Usage
Make a form
package main
import (
"github.com/semihs/goform"
"net/http"
"text/template"
)
var view string = `
<form method="post" action="">
{{range .GetElements}}
{{.Render}}
{{end}}
</form>
`
func main() {
email := goform.NewEmailElement("email", "Email", []*goform.Attribute{}, []goform.ValidatorInterface{
&goform.RequiredValidator{},
}, []goform.FilterInterface{})
password := goform.NewPasswordElement("password", "Password", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
submit := goform.NewButtonElement("submit", "Login", []*goform.Attribute{})
form := goform.NewGoForm()
form.Add(email)
form.Add(password)
form.Add(submit)
tpl, _ := template.New("tpl").Parse(view)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if r.Method != "POST" {
tpl.Execute(w, form)
return
}
r.ParseForm()
form.BindFromRequest(r)
if !form.IsValid() {
tpl.Execute(w, form)
return
}
})
http.ListenAndServe(":2626", nil)
}
Bind From Request
package main
import (
"github.com/semihs/goform"
"net/http"
"text/template"
)
var view string = `
<form method="post" action="">
{{range .GetElements}}
{{.Render}}
{{end}}
</form>
`
func main() {
email := goform.NewEmailElement("email", "Email", []*goform.Attribute{}, []goform.ValidatorInterface{
&goform.RequiredValidator{},
}, []goform.FilterInterface{})
password := goform.NewPasswordElement("password", "Password", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
submit := goform.NewButtonElement("submit", "Login", []*goform.Attribute{})
form := goform.NewGoForm()
form.Add(email)
form.Add(password)
form.Add(submit)
tpl, _ := template.New("tpl").Parse(view)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if r.Method != "POST" {
tpl.Execute(w, form)
return
}
r.ParseForm()
form.BindFromRequest(r)
tpl.Execute(w, form)
return
})
http.ListenAndServe(":2626", nil)
}
Bind From Interface
package main
import (
"github.com/semihs/goform"
"net/http"
"text/template"
)
var view string = `
<form method="post" action="">
{{range .GetElements}}
{{.Render}}
{{end}}
</form>
`
type YourInterface struct {
Email string
Password string
}
func main() {
email := goform.NewEmailElement("email", "Email", []*goform.Attribute{}, []goform.ValidatorInterface{
&goform.RequiredValidator{},
}, []goform.FilterInterface{})
password := goform.NewPasswordElement("password", "Password", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
submit := goform.NewButtonElement("submit", "Login", []*goform.Attribute{})
form := goform.NewGoForm()
form.Add(email)
form.Add(password)
form.Add(submit)
tpl, _ := template.New("tpl").Parse(view)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if r.Method != "POST" {
s := YourInterface{
Email: "[email protected]",
Password: "password",
}
form.BindFromInterface(s)
tpl.Execute(w, form)
return
}
})
http.ListenAndServe(":2626", nil)
}
Map to your struct
package main
import (
"fmt"
"github.com/semihs/goform"
"net/http"
"text/template"
)
var view string = `
<form method="post" action="">
{{range .GetElements}}
{{.Render}}
{{end}}
</form>
`
type YourStruct struct {
Email string
Password string
}
func main() {
email := goform.NewEmailElement("email", "Email", []*goform.Attribute{}, []goform.ValidatorInterface{
&goform.RequiredValidator{},
}, []goform.FilterInterface{})
password := goform.NewPasswordElement("password", "Password", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
submit := goform.NewButtonElement("submit", "Login", []*goform.Attribute{})
form := goform.NewGoForm()
form.Add(email)
form.Add(password)
form.Add(submit)
tpl, _ := template.New("tpl").Parse(view)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if r.Method != "POST" {
tpl.Execute(w, form)
return
}
r.ParseForm()
form.BindFromRequest(r)
s := YourStruct{}
form.MapTo(&s)
fmt.Println(s)
})
http.ListenAndServe(":2626", nil)
}
Validate a request
package main
import (
"github.com/semihs/goform"
"net/http"
"text/template"
)
var view string = `
<form method="post" action="">
{{range .GetElements}}
{{.Render}}
{{end}}
</form>
`
func main() {
email := goform.NewEmailElement("email", "Email", []*goform.Attribute{}, []goform.ValidatorInterface{
&goform.RequiredValidator{},
}, []goform.FilterInterface{})
password := goform.NewPasswordElement("password", "Password", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
submit := goform.NewButtonElement("submit", "Login", []*goform.Attribute{})
form := goform.NewGoForm()
form.Add(email)
form.Add(password)
form.Add(submit)
tpl, _ := template.New("tpl").Parse(view)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if r.Method != "POST" {
tpl.Execute(w, form)
return
}
r.ParseForm()
form.BindFromRequest(r)
if !form.IsValid() {
tpl.Execute(w, form)
return
}
})
http.ListenAndServe(":2626", nil)
}
Change Template
goform provides bootstrap 4 alpha textual and inline templates, if you want to make custom template look at the template.go and use SetTemplate method form. Your template must be goform.Theme type
package main
import (
"github.com/semihs/goform"
"net/http"
"text/template"
)
var view string = `
<form method="post" action="">
{{range .GetElements}}
{{.Render}}
{{end}}
</form>
`
func main() {
email := goform.NewEmailElement("email", "Email", []*goform.Attribute{}, []goform.ValidatorInterface{
&goform.RequiredValidator{},
}, []goform.FilterInterface{})
password := goform.NewPasswordElement("password", "Password", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
submit := goform.NewButtonElement("submit", "Login", []*goform.Attribute{})
form := goform.NewGoForm()
form.SetTheme(goform.ThemeBootstrap4alpha6Inline)
form.Add(email)
form.Add(password)
form.Add(submit)
tpl, _ := template.New("tpl").Parse(view)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if r.Method != "POST" {
tpl.Execute(w, form)
return
}
})
http.ListenAndServe(":2626", nil)
}
Build Query
package main
import (
"fmt"
"github.com/semihs/goform"
"net/http"
"text/template"
)
var view string = `
<form method="post" action="">
{{range .GetElements}}
{{.Render}}
{{end}}
</form>
`
func main() {
email := goform.NewEmailElement("email", "Email", []*goform.Attribute{}, []goform.ValidatorInterface{
&goform.RequiredValidator{},
}, []goform.FilterInterface{})
password := goform.NewPasswordElement("password", "Password", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
submit := goform.NewButtonElement("submit", "Login", []*goform.Attribute{})
form := goform.NewGoForm()
form.Add(email)
form.Add(password)
form.Add(submit)
tpl, _ := template.New("tpl").Parse(view)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if r.Method != "POST" {
tpl.Execute(w, form)
return
}
r.ParseForm()
form.BindFromRequest(r)
fmt.Println(form.BuildQuery())
})
http.ListenAndServe(":2626", nil)
}
Elements
Text Element
goform.NewTextElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Textarea Element
goform.NewTextareaElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Email Element
goform.NewEmailElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Checkbox Element
goform.NewCheckboxElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Select Element
goform.NewSelectElement("element_name", "Element Label", []*goform.Attribute{}, []*goform.ValueOption{
&goform.ValueOption{Value: "1", Label: "Option 1"},
&goform.ValueOption{Value: "2", Label: "Option 2"},
}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Radio Element
goform.NewRadioElement("element_name", "Element Label", []*goform.Attribute{}, []*goform.ValueOption{
&goform.ValueOption{Value: "1", Label: "Option 1"},
&goform.ValueOption{Value: "2", Label: "Option 2"},
}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Multicheckbox Element
goform.NewMultiCheckboxElement("element_name", "Element Label", []*goform.Attribute{}, []*goform.ValueOption{
&goform.ValueOption{Value: "1", Label: "Option 1"},
&goform.ValueOption{Value: "2", Label: "Option 2"},
}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Number Element
goform.NewNumberElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Search Element
goform.NewSearchElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Tel Element
goform.NewTelElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Hidden Element
goform.NewHiddenElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Password Element
goform.NewPasswordElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Image Element
goform.NewImageElement("element_name", "Element Label", []*goform.Attribute{
&goform.Attribute{Key:"src", Value: "/img/src/image.png"}
}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
Button Element
goform.NewButtonElement("submit", "Save", []*goform.Attribute{})
Submit Element
goform.NewSubmitElement("submit", "Save", []*goform.Attribute{})
Todo List
- Input Filters (tolower, toupper, alpha, numeric...)
- Validations (identical, min-max length, min-max value, alpha, regex...)
- Tests