From 1d40aa6b09135368915c10216dcb11dce98bb05c Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 1 Oct 2025 18:13:43 +0200 Subject: [PATCH] refactor: refactor the example --- example.go | 76 +++++++++++++ example/form.go | 32 +++--- example/view/bootstrap.html | 113 +++++++++++++++++++ example/view/html5.html | 133 ++++++++++++++++++++++ main.go | 219 ------------------------------------ 5 files changed, 338 insertions(+), 235 deletions(-) create mode 100644 example.go create mode 100644 example/view/bootstrap.html create mode 100644 example/view/html5.html delete mode 100644 main.go diff --git a/example.go b/example.go new file mode 100644 index 0000000..2aa6727 --- /dev/null +++ b/example.go @@ -0,0 +1,76 @@ +package main + +import ( + "embed" + "encoding/json" + "html/template" + "log" + "net/http" + + "github.com/yassinebenaid/godump" + "gitnet.fr/deblan/go-form/example" + "gitnet.fr/deblan/go-form/theme" +) + +//go:embed example/view/*.html +var templates embed.FS + +func handler(view, action string, formRenderer *theme.Renderer, w http.ResponseWriter, r *http.Request) { + entity := example.ExampleData{} + form := example.CreateDataForm(action) + + form.Mount(entity) + + if r.Method == form.Method { + form.HandleRequest(r) + + if form.IsSubmitted() && form.IsValid() { + form.Bind(&entity) + } + } + + content, _ := templates.ReadFile(view) + + formAsJson, _ := json.MarshalIndent(form, " ", " ") + + tpl, _ := template.New("page"). + Funcs(formRenderer.FuncMap()). + Parse(string(content)) + + w.Header().Set("Content-Type", "text/html; charset=utf-8") + + var dump godump.Dumper + dump.Theme = godump.Theme{} + + tpl.Execute(w, map[string]any{ + "isSubmitted": form.IsSubmitted(), + "isValid": form.IsValid(), + "form": form, + "json": string(formAsJson), + "dump": template.HTML(dump.Sprint(entity)), + }) +} + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + handler( + "example/view/html5.html", + "/", + theme.NewRenderer(theme.Html5), + w, + r, + ) + }) + + http.HandleFunc("/bootstrap", func(w http.ResponseWriter, r *http.Request) { + handler( + "example/view/bootstrap.html", + "/bootstrap", + theme.NewRenderer(theme.Bootstrap5), + w, + r, + ) + }) + + log.Fatal(http.ListenAndServe(":1122", nil)) +} diff --git a/example/form.go b/example/form.go index 4fd035c..8c516ba 100644 --- a/example/form.go +++ b/example/form.go @@ -50,7 +50,7 @@ type CollectionItem struct { ValueB string } -func CreateDataForm() *form.Form { +func CreateDataForm(action string) *form.Form { items := []Item{ Item{Id: 1, Name: "Item 1"}, Item{Id: 2, Name: "Item 2"}, @@ -66,19 +66,6 @@ func CreateDataForm() *form.Form { }) return form.NewForm( - form.NewFieldCollection("Collection"). - WithOptions( - form.NewOption("label", "Collection"), - form.NewOption("form", form.NewForm( - form.NewFieldText("ValueA"). - WithOptions(form.NewOption("label", "Value A")). - WithConstraints( - validation.NewNotBlank(), - ), - form.NewFieldText("ValueB"). - WithOptions(form.NewOption("label", "Value B")), - )), - ), form.NewFieldText("Bytes"). WithOptions( form.NewOption("label", "Bytes"), @@ -196,6 +183,19 @@ func CreateDataForm() *form.Form { form.NewOption("multiple", true), ), ), + form.NewFieldCollection("Collection"). + WithOptions( + form.NewOption("label", "Collection"), + form.NewOption("form", form.NewForm( + form.NewFieldText("ValueA"). + WithOptions(form.NewOption("label", "Value A")). + WithConstraints( + validation.NewNotBlank(), + ), + form.NewFieldText("ValueB"). + WithOptions(form.NewOption("label", "Value B")), + )), + ), form.NewFieldCsrf("_csrf_token").WithData("my-token"), form.NewSubmit("submit"). WithOptions( @@ -206,8 +206,8 @@ func CreateDataForm() *form.Form { ). End(). WithOptions( - form.NewOption("help", "Form help"), + form.NewOption("help", "Form global help"), ). WithMethod(http.MethodPost). - WithAction("/") + WithAction(action) } diff --git a/example/view/bootstrap.html b/example/view/bootstrap.html new file mode 100644 index 0000000..7b848f1 --- /dev/null +++ b/example/view/bootstrap.html @@ -0,0 +1,113 @@ + + + + + Form with Bootstrap + + + + +
+
+ Debug view +
+ Submitted: + {{ .isSubmitted }} +
+
+ Valid: + {{ .isValid }} +
+ +
+ Dump of data +
{{ .dump }}
+
+ +
+ Form as JSON +
{{ .json }}
+
+
+ + {{if .isValid}} +
The form is valid!
+ {{else}} +
The form is invalid!
+ {{end}} + + {{ form .form }} +
+ + + + diff --git a/example/view/html5.html b/example/view/html5.html new file mode 100644 index 0000000..90c33fb --- /dev/null +++ b/example/view/html5.html @@ -0,0 +1,133 @@ + + + + + Form HTML5 (with Pico) + + + + + + + + +
+ Debug view + +
+ Submitted: + {{ .isSubmitted }} +
+
+ Valid: + {{ .isValid }} +
+ +
+ Dump of data +
{{ .dump }}
+
+ +
+ Form as JSON +
{{ .json }}
+
+ +
+ + {{if .isValid}} +

The form is valid!

+ {{else}} +

The form is invalid!

+ {{end}} + + {{ form .form }} + + + + + diff --git a/main.go b/main.go deleted file mode 100644 index 7c4e97a..0000000 --- a/main.go +++ /dev/null @@ -1,219 +0,0 @@ -package main - -import ( - "encoding/json" - "html/template" - "log" - "net/http" - - "github.com/yassinebenaid/godump" - "gitnet.fr/deblan/go-form/example" - "gitnet.fr/deblan/go-form/theme" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - data := example.ExampleData{} - data.Collection = []example.CollectionItem{ - {"Value a 1", "Value b 1"}, - } - - f := example.CreateDataForm() - f.Mount(data) - - if r.Method == f.Method { - f.HandleRequest(r) - - if f.IsSubmitted() && f.IsValid() { - f.Bind(&data) - // godump.Dump(data) - } - } - - render := theme.NewRenderer(theme.Html5) - - tpl, _ := template.New("page").Funcs(render.FuncMap()).Parse(` - - - - - Form - - - -
-
- Submitted - {{ .Form.IsSubmitted }} -
-
- Valid - {{ .Form.IsValid }} -
- - - - - -
-
{{ .Dump }}
-
-
- JSON -
{{ .Json }}
-
-
-
- - {{ form .Form }} - - - - - `) - - var dump godump.Dumper - dump.Theme = godump.Theme{} - - j, _ := json.MarshalIndent(f, " ", " ") - - w.Header().Set("Content-Type", "text/html; charset=utf-8") - tpl.Execute(w, map[string]any{ - "Form": f, - "Json": string(j), - "Dump": template.HTML(dump.Sprint(data)), - }) - }) - - http.HandleFunc("/bootstrap", func(w http.ResponseWriter, r *http.Request) { - data := example.ExampleData{} - - f := example.CreateDataForm() - f.WithAction("/bootstrap") - f.Mount(data) - - if r.Method == f.Method { - f.HandleRequest(r) - - if f.IsSubmitted() && f.IsValid() { - f.Bind(&data) - godump.Dump(data) - } - } - - render := theme.NewRenderer(theme.Bootstrap5) - - tpl, _ := template.New("page").Funcs(render.FuncMap()).Parse(` - - - - - Form - - - -
-
-
- Submitted - {{ .Form.IsSubmitted }} -
-
- Valid - {{ .Form.IsValid }} -
-
- Data -
{{ .Dump }}
-
-
- - {{ form .Form }} -
- - - `) - - var dump godump.Dumper - dump.Theme = godump.Theme{} - - w.Header().Set("Content-Type", "text/html; charset=utf-8") - tpl.Execute(w, map[string]any{ - "Form": f, - "Dump": template.HTML(dump.Sprint(data)), - }) - }) - - log.Fatal(http.ListenAndServe(":1122", nil)) -}