go-form/en.search-data.json
2025-07-28 19:48:37 +00:00

1 line
No EOL
14 KiB
JSON
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{"/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/constraints/":{"data":{"":"The validation is designed to validate data against constraints.","constraints#Constraints":"Length Validate the length of an array, a slice or a string\nc := validation.NewLength() // Define minimum c.WithMin(1) // Define minimum c.WithMax(100) // Define min and max // Equivalent to c.WithMin(50).WithMax(50) c.WithExact(50) Mail validation.NewMail() Not blank validation.NewNotBlank() Range Validate a number\nc := validation.NewRange() // Define minimum c.WithMin(1) // Define minimum c.WithMax(100) // Define min and max // Equivalent to c.WithMin(1).WithMax(100) c.WithRange(1, 100) Regex Validate a string with a regex\nc := validation.NewRegex(`expression`) // The value must match c.MustMatch() // The value must not match c.MustNotMatch() Is even Validate that a number is even.\nvalidation.NewIsEven() Is odd Validate that a number is odd.\nvalidation.NewIsOdd() ","custom-constraint#Custom constraint":"Use case: you want to validate that the data equals “example”\npackage validation import ( \"reflect\" v \"gitnet.fr/deblan/go-form/validation\" ) // Define a struct type IsExample struct { Message string TypeErrorMessage string } // Create a factory func NewIsExample() IsEven { return IsEven{ Message: \"This value does not equal \\\"example\\\".\", TypeErrorMessage: \"This value can not be processed.\", } } // Implement the validation func (c IsExample) Validate(data any) []Error { errors := []Error{} // Should not validate blank data if len(v.NewNotBlank().Validate(data)) != 0 { return []Error{} } t := reflect.TypeOf(data) if t.Kind() == reflect.Ptr { t = t.Elem() } switch t.Kind() { case reflect.String: if data.(string) != \"example\" { errors = append(errors, Error(c.Message)) } default: errors = append(errors, Error(c.TypeErrorMessage)) } return errors } ","import#Import":" import ( \"gitnet.fr/deblan/go-form/validation\" ) "},"title":"Constraints"},"/go-form/docs/fields/":{"data":{"":"","#":"A field represents a field in a form.\nCheckbox func NewFieldCheckbox(name string) *Field field := form.NewFieldCheckbox(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=checkbox]\nChoice func NewFieldChoice(name string) *Field field := form.NewFieldChoice(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates inputs (checkbox or radio) or selects\nCsrf func NewFieldCsrf(name string) *Field field := form.NewFieldCsrf(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Date func NewFieldDate(name string) *Field field := form.NewFieldDate(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=date] with default transformers\nDatetime func NewFieldDatetime(name string) *Field field := form.NewFieldDatetime(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=datetime] with default transformers\nDatetimeLocal func NewFieldDatetimeLocal(name string) *Field field := form.NewFieldDatetimeLocal(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=datetime-local] with default transformers\nHidden func NewFieldHidden(name string) *Field field := form.NewFieldHidden(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=hidden]\nMail func NewFieldMail(name string) *Field field := form.NewFieldMail(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=email]\nNumber func NewFieldNumber(name string) *Field field := form.NewFieldNumber(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=number] with default transformers\nPassword func NewFieldPassword(name string) *Field field := form.NewFieldPassword(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=password]\nRange func NewFieldRange(name string) *Field field := form.NewFieldRange(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=range]\nSub Form func NewFieldSubForm(name string) *Field field := form.NewFieldSubForm(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Alias:\nfunc NewSubForm(name string) *Field Generates a sub form\nText func NewFieldText(name string) *Field field := form.NewFieldText(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=text]\nTextarea func NewFieldTextarea(name string) *Field field := form.NewFieldTextarea(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates a textarea\nTime func NewFieldTime(name string) *Field field := form.NewFieldTime(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=time] with default transformers\nSubmit func NewSubmit(name string) *Field field := form.NewSubmit(\"Foo\") Test me Run Try it yourself ↗ Share ↗ Generates an input[type=submit]","methods#Methods":"Add func (f *Field) Add(children ...*Field) *Field Appends children\nBind func (f *Field) Bind(data map[string]any, key *string) error Bind the data into the given map\nGetChild func (f *Field) GetChild(name string) *Field Returns a child using its name\nGetId func (f *Field) GetId() string Computes the id of the field\nGetName func (f *Field) GetName() string Computes the name of the field\nGetOption func (f *Field) GetOption(name string) *Option Returns an option using its name\nHasChild func (f *Field) HasChild(name string) bool Checks if the field contains a child using its name\nHasOption func (f *Field) HasOption(name string) bool Checks if the field contains an option using its name\nMount func (f *Field) Mount(data any) error Populates the field with data\nResetErrors func (f *Field) ResetErrors() *Field Resets the field errors\nWithBeforeBind func (f *Field) WithBeforeBind(callback func(data any) (any, error)) *Field Sets a transformer applied to the data of a field before defining it in a structure\nWithBeforeMount func (f *Field) WithBeforeMount(callback func(data any) (any, error)) *Field Sets a transformer applied to the structure data before displaying it in a field\nWithConstraints func (f *Field) WithConstraints(constraints ...validation.Constraint) *Field Appends constraints\nWithData func (f *Field) WithData(data any) *Field Sets data the field\nWithFixedName func (f *Field) WithFixedName() *Field Sets that the name of the field is not computed\nWithOptions func (f *Field) WithOptions(options ...*Option) *Field Common options Name type description Info required bool Add required=\"true\" Does not apply a constraint attr form.Attrs List of extra attributes of the field row_attr form.Attrs List of extra attributes of the fields top container label string The label of the field Usually show before the field label_attr form.Attrs List of extra attributes of the label help string Helper of the field help_attr form.Attrs List of extra attributes of the help Appends options to the field\nWithSlice func (f *Field) WithSlice() *Field Sets that the field represents a data slice"},"title":"Fields"},"/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\"). WithConstraints( validation.NewNotBlank(), ), form.NewFieldNumber(\"Age\"). WithConstraints( validation.NewNotBlank(), 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 attr map[string]string List of extra attributes help string Helper ","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/rendering/":{"data":{"":"go-form allows you to render a form using Gos built-in template engine. Here is a simple example that displays a form:\nmyForm := form.NewForm(...) render := theme.NewRenderer(theme.Bootstrap5) tpl, _ := template.New(\"page\").Funcs(render.FuncMap()).Parse(` \u003chtml\u003e \u003chead\u003e \u003ctitle\u003eMy form\u003c/title\u003e \u003c/head\u003e \u003cbody\u003e {{ form .Form }} \u003c/body\u003e \u003c/html\u003e `) b := new(strings.Builder) tpl.Execute(w, map[string]any{ \"Form\": myForm, }) fmt.Println(b.String()) @import \"fmt\" @import \"html/template\" @import \"strings\" @import @import \"github.com/yosssi/gohtml\" @import \"gitnet.fr/deblan/go-form/example\" @import \"gitnet.fr/deblan/go-form/theme\" form := example.CreateDataForm() render := theme.NewRenderer(theme.Html5) // render := theme.NewRenderer(theme.Bootstrap5) tpl, _ := template.New(\"page\").Funcs(render.FuncMap()).Parse(`{{ form .Form }}`) buff := new(strings.Builder) tpl.Execute(buff, map[string]any{ \"Form\": form, }) fmt.Println(gohtml.Format(buff.String())) Test me Run Try it yourself ↗ Share ↗ Other helper functions are available to render specific parts of the form:\nform_errors: displays the forms global errors form_row : renders the label, errors, and widget of a field form_label: renders only the label of a field form_widget: renders only the widget of a field form_widget_errors: renders only the errors of a specific field "},"title":"Rendering"},"/go-form/docs/rendering/theming/":{"data":{"":"go-form provides 2 themes:\ntheme.Html5: a basic view without classes theme.Bootstrap5: a theme for Bootstrap 5 You can add a custom theme. Learn by reading the Bootstrap5 theme."},"title":"Theming"},"/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\", form.Attrs{ \"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"}}