Create and process forms with Golang! https://deblan.gitnet.page/go-form/
  • Go 91.9%
  • HTML 8.1%
Find a file
2025-07-29 10:34:12 +02:00
example feat(example): remove boostrap classes 2025-07-28 21:53:43 +02:00
form fix(form): replace existing option in WithOptions 2025-07-29 10:14:43 +02:00
theme fix(theme/html5): remove div wrapper on form content 2025-07-29 10:34:04 +02:00
util add licence 2025-07-20 19:27:54 +02:00
validation refactor: refactor constraint (not blank check) 2025-07-22 21:10:58 +02:00
.gitignore feat: improve form rendering 2025-07-16 19:04:31 +02:00
CHANGELOG.md update changelog 2025-07-29 10:34:12 +02:00
doc.md feat: add boostrap5 theme 2025-07-26 19:48:15 +02:00
go.mod feat: replace templates with components 2025-07-26 12:48:04 +02:00
go.sum feat: replace templates with components 2025-07-26 12:48:04 +02:00
LICENCE add licence 2025-07-20 19:24:58 +02:00
main.go fix: remove useless code 2025-07-29 10:13:18 +02:00
README.md doc: add submit btn 2025-07-20 18:40:41 +02:00

go-form

Creating and processing HTML forms is hard and repetitive. You need to deal with rendering HTML form fields, validating submitted data, mapping the form data into objects and a lot more. go-form includes a powerful form feature that provides all these features.

Introduction

go-form is heavily influenced by Symfony Form. It includes:

  • A form builder based on fields declarations and independent of structs
  • Validation based on constraints
  • Data mounting to populate a form from a struct instance
  • Data binding to populate a struct instance from a submitted form
  • Form renderer with customizable themes

Installation

go get gitnet.fr/deblan/go-form

Quick Start

package main

import (
	"html/template"
	"log"
	"net/http"

	"gitnet.fr/deblan/go-form/form"
	"gitnet.fr/deblan/go-form/theme"
	"gitnet.fr/deblan/go-form/validation"
)

func main() {
	type Person struct {
		Name string
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		data := new(Person)

		f := form.NewForm(
			form.NewFieldText("Name").
				WithOptions(
					form.NewOption("label", "Your name"),
				).
				WithConstraints(
					validation.NewNotBlank(),
				),
		    form.NewSubmit("submit"),
		).
			End().
			WithMethod(http.MethodPost).
			WithAction("/")

		f.Mount(data)

		if r.Method == f.Method {
			f.HandleRequest(r)

			if f.IsSubmitted() && f.IsValid() {
				f.Bind(data)
			}
		}

		render := theme.NewRenderer(theme.Html5)
		tpl, _ := template.New("page").Funcs(render.FuncMap()).Parse(`{{ form .Form }}`)

		w.Header().Set("Content-Type", "text/html; charset=utf-8")
		tpl.Execute(w, map[string]any{
			"Form": f,
		})
	})

	log.Fatal(http.ListenAndServe(":1324", nil))
}