1 line
No EOL
7.9 KiB
JSON
1 line
No EOL
7.9 KiB
JSON
{"/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"}} |