Documentation
¶
Overview ¶
Package form is used to generate HTML forms. Most notably, the form.Builder makes it very easy to take a struct with set values and generate the input tags, labels, etc for each field in the struct.
See the examples directory for a more comprehensive idea of what can be\ accomplished with this package.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorsStub ¶
func ErrorsStub() []string
ErrorsStub is a stubbed out function that simply returns nil. It is present to make it a little easier to build the InputTemplate field of the Builder type, since your template will likely use the errors function in the template before it can truly be defined. You probably just want to use the provided FuncMap helper, but this can be useful when you need to build your own template.FuncMap.
See examples/errors/errors.go for a clear example of the FuncMap function being used, and see FuncMap for an example of how ErrorsStub can be used.
func FuncMap ¶
FuncMap is present to make it a little easier to build the InputTemplate field of the Builder type. In order to parse a template that uses the `errors` function, you need to have that template defined when the template is parsed. We clearly don't know whether a field has an error or not until it is parsed via the Inputs method call, so this basically just provides a stubbed out errors function that returns nil so the template compiles correctly.
See examples/errors/errors.go for a clear example of this being used.
Types ¶
type Builder ¶
Builder is used to build HTML forms/inputs for Go structs. Basic usage looks something like this:
tpl := template.Must(template.New("").Parse(` <input type="{{.Type}}" name="{{.Name}}"" {{with .Value}}value="{{.}}"{{end}}> `)) fb := Builder{InputTemplate: tpl} html := fb.Inputs(struct{ Name string `form:"name=full-name"` Email string `form:"type=email"` }{"Michael Scott", "michael@dundermifflin.com"}) // Outputs: // <input type="text" name="full-name" value="Michael Scott"> // <input type="email" name="Email" value="michael@dundermifflin.com">
This is a VERY limited example, but should demonstrate the basic idea. The Builder uses a single template to and will call it with all the information about each individual field and return the resulting HTML.
The most common use for this is to provide a helper function for your HTML templates. Eg something like:
fb := Builder{...} tpl, err := template.New("").Funcs(template.FuncMap{ "inputs_for": fb.Inputs, }) // Then later in a template: <form> {{inputs_for .SomeStruct}} </form>
For a much more thorough set of examples, see the examples directory. There is even an example illustrating how the gorilla/schema package can be used to parse forms that are created by the Builder.
func (*Builder) FuncMap ¶
FuncMap returns a template.FuncMap that defines both the inputs_for and inputs_and_errors_for functions for usage in the template package. The latter is provided via a closure because variadic parameters and the template package don't play very nicely and this just simplifies things a lot for end users of the form package.
func (*Builder) Inputs ¶
Inputs will parse the provided struct into fields and then execute the Builder.InputTemplate with each field. The returned HTML is simply all of these results appended one after another.
Inputs only accepts structs for the first argument. This may change later, but that is all I needed for my use case so it is what it does. If you need support for something else like maps let me know.
Inputs' second argument - errs - will be used to render errors for individual fields. This is done by looking for errors that implement the fieldError interface:
type fieldError interface { FieldError() (field, err string) }
Where the first return value is expected to be the field with an error and the second is the actual error message to be displayed. This is then used to provide an `errors` template function that will return a slice of errors (if there are any) for the current field your InputTemplate is rendering. See examples/errors/errors.go for an example of this in action.
This interface is not exported and you can pass other errors into Inputs but they currently won't be used.