Golang Web Forms validations, rendering and binding.
- HTML5 Support
- Bind From Interface
- Bind From Request
- Map to your struct
- Validation
- Build Query String
- Template
go get github.com/semihs/goform
See examples.
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)
}
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)
}
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: "semihsari@gmail.com",
Password: "password",
}
form.BindFromInterface(s)
tpl.Execute(w, form)
return
}
})
http.ListenAndServe(":2626", nil)
}
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)
}
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)
}
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)
}
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)
}
goform.NewTextElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
goform.NewTextareaElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
goform.NewEmailElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
goform.NewCheckboxElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
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{})
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{})
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{})
goform.NewNumberElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
goform.NewSearchElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
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{})
goform.NewPasswordElement("element_name", "Element Label", []*goform.Attribute{}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
goform.NewImageElement("element_name", "Element Label", []*goform.Attribute{
&goform.Attribute{Key:"src", Value: "/img/src/image.png"}
}, []goform.ValidatorInterface{}, []goform.FilterInterface{})
goform.NewButtonElement("submit", "Save", []*goform.Attribute{})
goform.NewSubmitElement("submit", "Save", []*goform.Attribute{})
- Input Filters (tolower, toupper, alpha, numeric...)
- Validations (identical, min-max length, min-max value, alpha, regex...)
- Tests