From 8a083f120412cedc25ebbf331583ca18e4484847 Mon Sep 17 00:00:00 2001 From: CI Date: Tue, 22 Jul 2025 14:19:03 +0000 Subject: [PATCH] Build doc --- categories/index.html | 24 +- docs/constraints/index.html | 47 +- docs/fields/button/index.html | 45 +- docs/fields/index.html | 45 +- docs/fields/input/date/index.html | 45 +- docs/fields/input/datetime/index.html | 45 +- docs/fields/input/hidden/index.html | 45 +- docs/fields/input/mail/index.html | 45 +- docs/fields/input/number/index.html | 45 +- docs/fields/input/password/index.html | 45 +- docs/fields/input/range/index.html | 45 +- docs/fields/input/text/index.html | 45 +- docs/fields/input/time/index.html | 45 +- docs/fields/textarea/index.html | 45 +- docs/form/index.html | 1032 +++++++++++++++++++++++++ docs/form/index.xml | 18 + docs/index.html | 77 +- docs/installation/index.html | 45 +- docs/rendering/index.html | 45 +- docs/rendering/theming/index.html | 45 +- docs/workflow/index.html | 45 +- en.search-data.json | 2 +- index.html | 24 +- sitemap.xml | 4 +- tags/index.html | 24 +- 25 files changed, 1647 insertions(+), 325 deletions(-) create mode 100644 docs/form/index.html create mode 100644 docs/form/index.xml diff --git a/categories/index.html b/categories/index.html index 082cdbc..2943a52 100644 --- a/categories/index.html +++ b/categories/index.html @@ -160,6 +160,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering diff --git a/docs/constraints/index.html b/docs/constraints/index.html index 1a81679..4e22565 100644 --- a/docs/constraints/index.html +++ b/docs/constraints/index.html @@ -162,6 +162,23 @@ >Workflow +
  • Form + + +
  • Constraints + + +
  • -
  • Constraints - - -
  • Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -310,6 +318,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/index.html b/docs/fields/index.html index dcc4a10..6ce26a5 100644 --- a/docs/fields/index.html +++ b/docs/fields/index.html @@ -164,6 +164,22 @@ import ( "bytes" "fmt" "html/template" "gitnet.fr/de href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -320,6 +328,20 @@ import ( "bytes" "fmt" "html/template" "gitnet.fr/de href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/input/date/index.html b/docs/fields/input/date/index.html index 4b4faf9..445f1c7 100644 --- a/docs/fields/input/date/index.html +++ b/docs/fields/input/date/index.html @@ -162,6 +162,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -310,6 +318,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/input/datetime/index.html b/docs/fields/input/datetime/index.html index 2bf9399..1347ad0 100644 --- a/docs/fields/input/datetime/index.html +++ b/docs/fields/input/datetime/index.html @@ -162,6 +162,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -310,6 +318,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/input/hidden/index.html b/docs/fields/input/hidden/index.html index e0a14bd..f28d15e 100644 --- a/docs/fields/input/hidden/index.html +++ b/docs/fields/input/hidden/index.html @@ -162,6 +162,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -310,6 +318,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/input/mail/index.html b/docs/fields/input/mail/index.html index 47045dd..56a40fe 100644 --- a/docs/fields/input/mail/index.html +++ b/docs/fields/input/mail/index.html @@ -167,6 +167,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -315,6 +323,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/input/number/index.html b/docs/fields/input/number/index.html index e86ff72..f178fbb 100644 --- a/docs/fields/input/number/index.html +++ b/docs/fields/input/number/index.html @@ -167,6 +167,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -315,6 +323,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/input/password/index.html b/docs/fields/input/password/index.html index 6edbafc..c4ff620 100644 --- a/docs/fields/input/password/index.html +++ b/docs/fields/input/password/index.html @@ -167,6 +167,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -315,6 +323,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/input/range/index.html b/docs/fields/input/range/index.html index 8883770..cec9346 100644 --- a/docs/fields/input/range/index.html +++ b/docs/fields/input/range/index.html @@ -167,6 +167,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -315,6 +323,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/input/text/index.html b/docs/fields/input/text/index.html index 250fff2..8222a90 100644 --- a/docs/fields/input/text/index.html +++ b/docs/fields/input/text/index.html @@ -229,6 +229,22 @@ form.NewFieldText("Name"). href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -391,6 +399,20 @@ form.NewFieldText("Name"). href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/input/time/index.html b/docs/fields/input/time/index.html index fe12846..85ac96e 100644 --- a/docs/fields/input/time/index.html +++ b/docs/fields/input/time/index.html @@ -162,6 +162,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -310,6 +318,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/fields/textarea/index.html b/docs/fields/textarea/index.html index e47e935..f3583a0 100644 --- a/docs/fields/textarea/index.html +++ b/docs/fields/textarea/index.html @@ -162,6 +162,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -310,6 +318,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/form/index.html b/docs/form/index.html new file mode 100644 index 0000000..e9b0711 --- /dev/null +++ b/docs/form/index.html @@ -0,0 +1,1032 @@ + + + + + + + + + + + + +Form – deblan/go-form + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    + + + + + +
    +
    + +
    +
    Form
    +
    + +
    +

    Form

    +

    Example +

    Prerequisites +

    + +
    import (
    +	"gitnet.fr/deblan/go-form/form"
    +	"gitnet.fr/deblan/go-form/validation"
    +)
    +
    +type Person struct {
    +    Name string
    +    Age  int
    +}
    + +
    +
    +

    Creating a form +

    + +
    myForm := form.NewForm(
    +    form.NewFieldText("Name").
    +        WithOptions(
    +            form.NewOption("label", "Person"),
    +            form.NewOption("required", true),
    +        ).
    +        WithConstraints(
    +            validation.NewNotBlank(),
    +        ),
    +    form.NewFieldNumber("Age").
    +        WithOptions(
    +            form.NewOption("label", "Age"),
    +            form.NewOption("required", true),
    +        ).
    +        WithConstraints(
    +            validation.NewNotBlank(),
    +            validation.New(),
    +            validation.NewRange().WithMin(18),
    +        ),
    +).End()
    + +
    +
    +

    Validating a struct +

    + +
    data := Person{}
    +
    +myForm.Mount(data)
    +myForm.IsValid() // false
    +
    +data = Person{
    +    Name: "Alice",
    +    Age:  42,
    +}
    +
    +myForm.Mount(data)
    +myForm.IsValid() // true
    + +
    +
    +

    Validating a request +

    + +
    import (
    +    "net/http"
    +)
    +
    +myForm.WithMethod(http.MethodPost)
    +
    +// req *http.Request
    +if req.Method == myForm.Method {
    +    myForm.HandleRequest(req)
    +
    +    if myForm.IsSubmitted() && myForm.IsValid() {
    +        myForm.Bind(&data)
    +    }
    +}
    + +
    +
    +

    Struct +

    + +
    type Form struct {
    +	Fields       []*Field
    +	GlobalFields []*Field
    +	Errors       []validation.Error
    +	Method       string
    +	Action       string
    +	Name         string
    +	Options      []*Option
    +	RequestData  *url.Values
    +}
    + +
    +
    +

    Methods +

    NewForm +

    + +
    func NewForm(fields ...*Field) *Form
    + +
    +
    +

    Generates a new form with default properties

    +

    Add +

    + +
    func (f *Form) Add(fields ...*Field)
    + +
    +
    +

    Appends children

    +

    AddGlobalField +

    + +
    func (f *Form) AddGlobalField(field *Field)
    + +
    +
    +

    Configures its children deeply

    +

    Bind +

    + +
    func (f *Form) Bind(data any) error
    + +
    +
    +

    Copies datas from the form to a struct

    +

    End +

    + +
    func (f *Form) End() *Form
    + +
    +
    +

    Configures its children deeply This function must be called after adding all

    +

    fields

    +

    GetField +

    + +
    func (f *Form) GetField(name string) *Field
    + +
    +
    +

    Returns a child using its name

    +

    GetOption +

    + +
    func (f *Form) GetOption(name string) *Option
    + +
    +
    +

    Returns an option using its name

    +

    HandleRequest +

    + +
    func (f *Form) HandleRequest(req *http.Request)
    + +
    +
    +

    Processes a request

    +

    HasField +

    + +
    func (f *Form) HasField(name string) bool
    + +
    +
    +

    Checks if the form contains a child using its name

    +

    HasOption +

    + +
    func (f *Form) HasOption(name string) bool
    + +
    +
    +

    Checks if the form contains an option using its name

    +

    IsSubmitted +

    + +
    func (f *Form) IsSubmitted() bool
    + +
    +
    +

    Checks if the form is submitted

    +

    IsValid +

    + +
    func (f *Form) IsValid() bool
    + +
    +
    +

    Checks the a form is valid

    +

    Mount +

    + +
    func (f *Form) Mount(data any) error
    + +
    +
    +

    Copies datas from a struct to the form

    +

    ResetErrors +

    + +
    func (f *Form) ResetErrors() *Form
    + +
    +
    +

    Resets the form errors

    +

    WithAction +

    + +
    func (f *Form) WithAction(v string) *Form
    + +
    +
    +

    Sets the action of the form (eg: “/”)

    +

    WithMethod +

    + +
    func (f *Form) WithMethod(v string) *Form
    + +
    +
    +

    Sets the method of the format (http.MethodPost, http.MethodGet, …)

    +

    WithName +

    + +
    func (f *Form) WithName(v string) *Form
    + +
    +
    +

    Sets the name of the form (used to compute name of fields)

    +

    WithOptions +

    + +
    func (f *Form) WithOptions(options ...*Option) *Form
    + +
    +
    +

    Appends options to the form

    +

    Options +

    + + + + + + + + + + + + + + + + +
    NameTypeDescriptionInfo
    attrmap[string]stringList of extra attributes
    + +
    +
    + + +
    +
    +
    + +
    + + + + + + diff --git a/docs/form/index.xml b/docs/form/index.xml new file mode 100644 index 0000000..4e02c53 --- /dev/null +++ b/docs/form/index.xml @@ -0,0 +1,18 @@ + + + deblan/go-form – Form + https://deblan.gitnet.page/go-form/docs/form/ + Recent content in Form on deblan/go-form + Hugo -- gohugo.io + en-us + + + + + + + + + + + diff --git a/docs/index.html b/docs/index.html index 72f386c..f5e6d08 100644 --- a/docs/index.html +++ b/docs/index.html @@ -24,7 +24,7 @@ A form builder based on fields declarations and independent of structs Validatio - + - -
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering @@ -319,6 +327,20 @@ go get gitnet.fr/deblan/go-form href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/docs/rendering/index.html b/docs/rendering/index.html index f32b02b..b1cba71 100644 --- a/docs/rendering/index.html +++ b/docs/rendering/index.html @@ -160,6 +160,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • -
  • Constraints - -
  • Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • Workflow + + +
  • Form + + +
  • Constraints
  • -
  • Constraints - -
  • Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • +
  • Form + + +
  • Constraints + +
  • Constraints - - -
  • Rendering @@ -317,6 +325,20 @@ href="/go-form/docs/workflow/" >Workflow +
  • +
  • Form +
  • +
  • Constraints
  • Constraints -
  • -
  • Rendering diff --git a/en.search-data.json b/en.search-data.json index fffb466..d2d8470 100644 --- a/en.search-data.json +++ b/en.search-data.json @@ -1 +1 @@ -{"/go-form/docs/":{"data":{"":"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.\ngo-form is heavily influenced by Symfony Form. It includes:\nA 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 ","questions-or-feedback#Questions or Feedback?":" fmt.Sprintf(\"foo\") "},"title":"Documentation"},"/go-form/docs/fields/":{"data":{"":"The documentation of field is realised with this program.\nimport ( \"bytes\" \"fmt\" \"html/template\" \"gitnet.fr/deblan/go-form/form\" \"gitnet.fr/deblan/go-form/theme\" ) func main() { myForm := form.NewForm( // the documented field here // form.NewField... ). WithName(\"form\"). End() render := theme.NewRenderer(theme.Html5) tpl, _ := template.New(\"example\").Funcs(render.FuncMap()).Parse(`{{- form .Form -}}`) var buffer bytes.Buffer tpl.Execute(\u0026buffer, map[string]any{ \"Form\": myForm, }) fmt.Print(buffer.String()) } ","common-options#Common options":" Name type description Info required bool Add required=\"true\" Does not apply a constraint attr map[string]string List of extra attributes of the field row_attr map[string]string List of extra attributes of the field’s top container label string The label of the field Usually show before the field label_attr map[string]string List of extra attributes of the label help string Helper of the field "},"title":"Fields"},"/go-form/docs/fields/input/mail/":{"data":{"":" ok "},"title":"Mail"},"/go-form/docs/fields/input/number/":{"data":{"":" ok "},"title":"Number"},"/go-form/docs/fields/input/password/":{"data":{"":" ok "},"title":"Password"},"/go-form/docs/fields/input/range/":{"data":{"":" ok "},"title":"Range"},"/go-form/docs/fields/input/text/":{"data":{"":"","basic-example#Basic example":" GOResult form.NewFieldText(\"Name\") \u003cform action=\"\" method=\"POST\" \u003e \u003cdiv \u003e \u003cinput id=\"form-name\" name=\"form[Name]\" value=\"\" type=\"text\" \u003e \u003c/div\u003e \u003c/form\u003e ","fully-featured-example#Fully featured example":" GOResult form.NewFieldText(\"Name\"). WithOptions( form.NewOption(\"label\", \"Name\"), form.NewOption(\"required\", true), form.NewOption(\"attr\", map[string]string{\"data-foo\": \"foo\"}), form.NewOption(\"row_attr\", map[string]string{\"data-bar\": \"bar\"}), ) \u003cform action=\"\" method=\"POST\" \u003e \u003cdiv data-bar=\"bar\"\u003e \u003clabel for=\"form-name\" \u003eName\u003c/label\u003e \u003cinput id=\"form-name\" required=\"required\" name=\"form[Name]\" value=\"\" type=\"text\" data-foo=\"foo\"\u003e \u003c/div\u003e \u003c/form\u003e "},"title":"Text"},"/go-form/docs/installation/":{"data":{"":" go get gitnet.fr/deblan/go-form "},"title":"Installation"},"/go-form/docs/workflow/":{"data":{"":"","#":" Import import ( \"html/template\" \"net/http\" \"gitnet.fr/deblan/go-form/form\" \"gitnet.fr/deblan/go-form/theme\" ) Create a form // Let's create a new form // You can pass *form.Field as arguments myForm := form.NewForm(field1, field2, ...) // Add somes fields myForm.Add(field3, field4, ...) // Set the method // \u003cform method=\"POST\" ...\u003e myForm.WithMethod(http.MethodPost) // Define the action // \u003cform action=\"/\" ...\u003e myForm.WithAction(\"/\") // Set a name myForm.WithName(\"myForm\") // Add options myForm.WithOptions(option1, option2, ...) // When all fields are added, call End() myForm.End() Attributes Some options are natively supported in go-form themes.\nmyForm.WithOptions( form.NewOption(\"help\", \"A help for the form\"), // \u003cform data-foo=\"bar\" data-bar=\"bar\" ... form.NewOption(\"attr\", map[string]string{ \"data-foo\": \"foo\", \"data-bar\": \"bar\", }), ) Data mounting This step is not required when you does not want to pre-fill the form. Your struct can be complexe and the form only map existing properties.\ntype Person struct { Name string Age int } data := Person{ Name: \"Alice\", Age: 42, } // Assuming 2 fields named \"Name\" and \"Age\" exist myForm.Mount(data) Rendering http.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) { // myForm := form.NewForm(...) 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\": myForm, }) } Data binding This is the final step. After the form handles the request, you can check if the form has been submitted, check the values are valid and finally populate your struct.\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) { // data := Person{...} // myForm := form.NewForm(...) if r.Method == myForm.Method { myForm.HandleRequest(r) if myForm.IsSubmitted() \u0026\u0026 myForm.IsValid() { myForm.Bind(\u0026data) } } }) "},"title":"Workflow"}} \ No newline at end of file +{"/go-form/docs/":{"data":{"":"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.\ngo-form is heavily influenced by Symfony Form. It includes:\nA 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 "},"title":"Documentation"},"/go-form/docs/fields/":{"data":{"":"The documentation of field is realised with this program.\nimport ( \"bytes\" \"fmt\" \"html/template\" \"gitnet.fr/deblan/go-form/form\" \"gitnet.fr/deblan/go-form/theme\" ) func main() { myForm := form.NewForm( // the documented field here // form.NewField... ). WithName(\"form\"). End() render := theme.NewRenderer(theme.Html5) tpl, _ := template.New(\"example\").Funcs(render.FuncMap()).Parse(`{{- form .Form -}}`) var buffer bytes.Buffer tpl.Execute(\u0026buffer, map[string]any{ \"Form\": myForm, }) fmt.Print(buffer.String()) } ","common-options#Common options":" Name type description Info required bool Add required=\"true\" Does not apply a constraint attr map[string]string List of extra attributes of the field row_attr map[string]string List of extra attributes of the field’s top container label string The label of the field Usually show before the field label_attr map[string]string List of extra attributes of the label help string Helper of the field "},"title":"Fields"},"/go-form/docs/fields/input/mail/":{"data":{"":" ok "},"title":"Mail"},"/go-form/docs/fields/input/number/":{"data":{"":" ok "},"title":"Number"},"/go-form/docs/fields/input/password/":{"data":{"":" ok "},"title":"Password"},"/go-form/docs/fields/input/range/":{"data":{"":" ok "},"title":"Range"},"/go-form/docs/fields/input/text/":{"data":{"":"","basic-example#Basic example":" GOResult form.NewFieldText(\"Name\") \u003cform action=\"\" method=\"POST\" \u003e \u003cdiv \u003e \u003cinput id=\"form-name\" name=\"form[Name]\" value=\"\" type=\"text\" \u003e \u003c/div\u003e \u003c/form\u003e ","fully-featured-example#Fully featured example":" GOResult form.NewFieldText(\"Name\"). WithOptions( form.NewOption(\"label\", \"Name\"), form.NewOption(\"required\", true), form.NewOption(\"attr\", map[string]string{\"data-foo\": \"foo\"}), form.NewOption(\"row_attr\", map[string]string{\"data-bar\": \"bar\"}), ) \u003cform action=\"\" method=\"POST\" \u003e \u003cdiv data-bar=\"bar\"\u003e \u003clabel for=\"form-name\" \u003eName\u003c/label\u003e \u003cinput id=\"form-name\" required=\"required\" name=\"form[Name]\" value=\"\" type=\"text\" data-foo=\"foo\"\u003e \u003c/div\u003e \u003c/form\u003e "},"title":"Text"},"/go-form/docs/form/":{"data":{"":"","example#Example":"Prerequisites import ( \"gitnet.fr/deblan/go-form/form\" \"gitnet.fr/deblan/go-form/validation\" ) type Person struct { Name string Age int } Creating a form myForm := form.NewForm( form.NewFieldText(\"Name\"). WithOptions( form.NewOption(\"label\", \"Person\"), form.NewOption(\"required\", true), ). WithConstraints( validation.NewNotBlank(), ), form.NewFieldNumber(\"Age\"). WithOptions( form.NewOption(\"label\", \"Age\"), form.NewOption(\"required\", true), ). WithConstraints( validation.NewNotBlank(), validation.New(), validation.NewRange().WithMin(18), ), ).End() Validating a struct data := Person{} myForm.Mount(data) myForm.IsValid() // false data = Person{ Name: \"Alice\", Age: 42, } myForm.Mount(data) myForm.IsValid() // true Validating a request import ( \"net/http\" ) myForm.WithMethod(http.MethodPost) // req *http.Request if req.Method == myForm.Method { myForm.HandleRequest(req) if myForm.IsSubmitted() \u0026\u0026 myForm.IsValid() { myForm.Bind(\u0026data) } } ","methods#Methods":"NewForm func NewForm(fields ...*Field) *Form Generates a new form with default properties\nAdd func (f *Form) Add(fields ...*Field) Appends children\nAddGlobalField func (f *Form) AddGlobalField(field *Field) Configures its children deeply\nBind func (f *Form) Bind(data any) error Copies datas from the form to a struct\nEnd func (f *Form) End() *Form Configures its children deeply This function must be called after adding all\nfields\nGetField func (f *Form) GetField(name string) *Field Returns a child using its name\nGetOption func (f *Form) GetOption(name string) *Option Returns an option using its name\nHandleRequest func (f *Form) HandleRequest(req *http.Request) Processes a request\nHasField func (f *Form) HasField(name string) bool Checks if the form contains a child using its name\nHasOption func (f *Form) HasOption(name string) bool Checks if the form contains an option using its name\nIsSubmitted func (f *Form) IsSubmitted() bool Checks if the form is submitted\nIsValid func (f *Form) IsValid() bool Checks the a form is valid\nMount func (f *Form) Mount(data any) error Copies datas from a struct to the form\nResetErrors func (f *Form) ResetErrors() *Form Resets the form errors\nWithAction func (f *Form) WithAction(v string) *Form Sets the action of the form (eg: “/”)\nWithMethod func (f *Form) WithMethod(v string) *Form Sets the method of the format (http.MethodPost, http.MethodGet, …)\nWithName func (f *Form) WithName(v string) *Form Sets the name of the form (used to compute name of fields)\nWithOptions func (f *Form) WithOptions(options ...*Option) *Form Appends options to the form\nOptions Name Type Description Info attr map[string]string List of extra attributes ","struct#Struct":" type Form struct { Fields []*Field GlobalFields []*Field Errors []validation.Error Method string Action string Name string Options []*Option RequestData *url.Values } "},"title":"Form"},"/go-form/docs/installation/":{"data":{"":" go get gitnet.fr/deblan/go-form "},"title":"Installation"},"/go-form/docs/workflow/":{"data":{"":"","#":" Import import ( \"html/template\" \"net/http\" \"gitnet.fr/deblan/go-form/form\" \"gitnet.fr/deblan/go-form/theme\" ) Create a form // Let's create a new form // You can pass *form.Field as arguments myForm := form.NewForm(field1, field2, ...) // Add somes fields myForm.Add(field3, field4, ...) // Set the method // \u003cform method=\"POST\" ...\u003e myForm.WithMethod(http.MethodPost) // Define the action // \u003cform action=\"/\" ...\u003e myForm.WithAction(\"/\") // Set a name myForm.WithName(\"myForm\") // Add options myForm.WithOptions(option1, option2, ...) // When all fields are added, call End() myForm.End() Attributes Some options are natively supported in go-form themes.\nmyForm.WithOptions( form.NewOption(\"help\", \"A help for the form\"), // \u003cform data-foo=\"bar\" data-bar=\"bar\" ... form.NewOption(\"attr\", map[string]string{ \"data-foo\": \"foo\", \"data-bar\": \"bar\", }), ) Data mounting This step is not required when you does not want to pre-fill the form. Your struct can be complexe and the form only map existing properties.\ntype Person struct { Name string Age int } data := Person{ Name: \"Alice\", Age: 42, } // Assuming 2 fields named \"Name\" and \"Age\" exist myForm.Mount(data) Rendering http.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) { // myForm := form.NewForm(...) 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\": myForm, }) } Data binding This is the final step. After the form handles the request, you can check if the form has been submitted, check the values are valid and finally populate your struct.\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) { // data := Person{...} // myForm := form.NewForm(...) if r.Method == myForm.Method { myForm.HandleRequest(r) if myForm.IsSubmitted() \u0026\u0026 myForm.IsValid() { myForm.Bind(\u0026data) } } }) "},"title":"Workflow"}} \ No newline at end of file diff --git a/index.html b/index.html index 555dd4f..5d7f89e 100644 --- a/index.html +++ b/index.html @@ -162,6 +162,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering diff --git a/sitemap.xml b/sitemap.xml index 29b158b..dcab1ff 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -10,9 +10,11 @@ https://deblan.gitnet.page/go-form/docs/workflow/ - https://deblan.gitnet.page/go-form/docs/fields/ + https://deblan.gitnet.page/go-form/docs/form/ https://deblan.gitnet.page/go-form/docs/constraints/ + + https://deblan.gitnet.page/go-form/docs/fields/ https://deblan.gitnet.page/go-form/docs/fields/button/ diff --git a/tags/index.html b/tags/index.html index 5af01b3..76292e7 100644 --- a/tags/index.html +++ b/tags/index.html @@ -160,6 +160,22 @@ href="/go-form/docs/workflow/" >Workflow + + +
  • Form + + +
  • Constraints
  • Constraints - - -
  • Rendering