Create and process forms with Golang! https://deblan.gitnet.page/go-form/
  • Go 91.9%
  • HTML 8.1%
Find a file
2025-07-20 19:24:58 +02:00
example doc: update example form configuration 2025-07-20 15:57:49 +02:00
form doc: add comments on functions and methods 2025-07-20 19:20:44 +02:00
theme feat: improve templates 2025-07-20 15:22:49 +02:00
util init 2025-07-16 16:43:26 +02:00
validation feat(validation/notblank): improve validation and messages 2025-07-20 15:24:45 +02:00
.gitignore feat: improve form rendering 2025-07-16 19:04:31 +02:00
CHANGELOG.md doc: add changelog 2025-07-20 15:57:05 +02:00
go.mod doc: improve example 2025-07-20 15:25:32 +02:00
go.sum doc: improve example 2025-07-20 15:25:32 +02:00
LICENCE add licence 2025-07-20 19:24:58 +02:00
main.go doc: improve example 2025-07-20 15:25:32 +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))
}