htmlform

package module
v0.0.0-...-e46316e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2015 License: MIT Imports: 3 Imported by: 7

README

htmlform GoDoc

Experimental: Not suitable for production use.

htmlform simplifies the interaction between web handlers, data models, decoding, validation and template logic.

Example

Example struct:

// using gorilla/schema and bluesuncorp/validator.v5

type registrationForm struct {
    Email string `validate:"required,email" schema:"email"`
    Password string `validate:"required,min=10" schema:"password" htmlform:"type=password"`
}

Call htmlform:

var model registrationForm
form := htmlform.Create(&model, nil, []string{}, []string{}).WithCSRF(nosurf.FormFieldName, nosurf.Token(req))

Returns this:

map[string]interface{}{
  "Email": FormField{
    "Name":          "email",
    "Type":          "text",
    "Value":         "",
    "Errors":        map[string]interface{}{},
  },
  "_CSRF": CSRF{
    "FieldName": "csrf_token",
    "Token":     "generated-csrf-token",
  },
}

Which you can use in a template:

{{ define "text_field" }}
    {{ if .Errors }}
        <strong>Error</strong>
    {{ end }}
    <input type="{{ .Type }}" name="{{ .Name }}" value="{{ .Value }}">
{{ end }}

{{ define "hidden_field" }}
    <input type="hidden" name="{{ .Name }}" value="{{ .Value }}">
{{ end }}

{{ template "text_field" .Form.Email }}
{{ template "hidden_field" .Form._CSRF }}
Errors

The second argument to htmlform.Create is a func(string) map[string]interface{}.

If set, it's called for each field to retrieve errors, making it easy to map errors from form validation into a structure useful for the template.

Example output from validator.v5:

errs := map[string]interface{}{
    "email": map[string]interface{
        "min": map[string]interface{
            "Param": "10",
        },
    },
}
errFunc := func(field string) map[string]interface{} {
  return errs[field]
}

Pass errFunc to htmlform.Create as the second argument.

From a template:

{{ define "form_error" }}
    {{ if .Errors }}
        {{ if .Errors.min }}{{ .Name }} must be at least {{ .Errors.min.Param }} characters{{ end }}
    {{ end }}
{{ end }}

{{ template "form_error" .Form.Email }}
Licence

Copyright ©‎ 2015, Ian Kent (http://iankent.uk).

Released under MIT license, see LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HtmlformStructTag = "htmlform"

HtmlformStructTag sets the struct tag used to set other form properties, e.g. setting the form field type to "password".

If the name= paramter is set, the value inferred or given by StructTag is replaced.

View Source
var NameStructTag = "schema"

NameStructTag sets the struct tag used to set the field name, e.g. schema If set to an empty string, the default name will be used.

Functions

func Arr

func Arr(args ...interface{}) []interface{}

Arr returns a slice for a given argument list

func Extend

func Extend(target map[string]interface{}, args ...interface{}) (map[string]interface{}, error)

Extend extends the target map with the provided arg pairs

func FirstNotNil

func FirstNotNil(args ...interface{}) interface{}

FirstNotNil returns th first parameter that isn't nil

func Map

func Map(args ...interface{}) (map[string]interface{}, error)

Map creates a map with the provided arg pairs

Types

type CSRF

type CSRF struct {
	FieldName string
	Token     string
}

CSRF is a HTML form field containing a CSRF token

func (CSRF) Name

func (c CSRF) Name() string

Name returns the form fields name

func (CSRF) Value

func (c CSRF) Value() string

Value returns the field value, formatted with fmt.Sprintf("%s")

type Errors

type Errors func(field string) map[string]interface{}

Errors is a function which, given a field name, returns a FieldError

type Form

type Form map[string]interface{}

Form is a HTML form

func Create

func Create(model interface{}, errs Errors, namespace []string, htmlNamespace []string) Form

Create returns a Form

func (Form) WithCSRF

func (f Form) WithCSRF(field, token string) Form

WithCSRF returns the form with a CSRF token

type FormField

type FormField map[string]interface{}

FormField is a HTML form field

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL