forms

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

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

Go to latest
Published: Nov 16, 2015 License: MIT Imports: 7 Imported by: 0

README

Forms

Build Status Coverage Status

A little library that allows you to control web forms through Go code. Currently in development

Usage

The following is a basic example of how to set up and use a go form

// Create the form
form := NewForm(http.Request)
form.Action = "/test/form/uri"
form.Method = "POST"

// Add fields
uname := &Field{
    Name:   "username",
    Label:  "Username",
    Type:   "text",
}

form.addField(uname)
YAML Form builder

You can create a yaml file to represent a form, passing this to builder will create a form with all its fields and validation rules specified, firstly you'll need to make a yaml form file, take a look at example.yml in the source code, after this pass it to the builder:

// Create a new builder
b := NewBuilder("example.yml", http.Request)
// Now the form can be built
b.build()

// Once the form is built you can access it through the builder
b.Form
b.Form.validate()
Output

The form struct will output the form tags, and each field can output the Html representation of itself, for example

form.open()

    for _, field := range form.Fields {
        field.output()
    }

form.close()
Validate

The form struct can also validate all fields it has, or you can validate individual fields...

// Upon adding a field the method will check the request
// object for the corresponding field value
form.validate()

// You can validate a single field as well
field.validate()

Both the form and field struct have the Valid boolean flag to signify whether they have passed validation or not

form.validate()

// true
return form.Valid
Validation rules

Remember to add the validation you need to the field, otherwise the result of validate will always be true!

rule := &Required{
    Err: "This field is required!"
}

field.addValidation(rule)

In the above example, if the field has no value and so fails validation it will add an entry into the fields Error list.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// The location of the YAML file
	Filename string
	// The extracted configuration settings
	Config Config
	// The form struct that will be populated from the yaml file
	Form *Form
	// Instance of the http request
	Request http.Request
}

The builder struct

func NewBuilder

func NewBuilder(file string, r http.Request) *Builder

Creates a new form builder and runs the extract method

type Config

type Config struct {
	Name        string            `yaml:"Name"`
	Description string            `yaml:"Description"`
	Action      string            `yaml:"Action"`
	Method      string            `yaml:"Method"`
	Props       map[string]string `yaml:"Props"`
	Fields      []ConfigField     `yaml:"Fields"`
}

The YAML config struct

type ConfigField

type ConfigField struct {
	Name    string            `yaml:"Name"`
	Label   string            `yaml:"Label"`
	Type    string            `yaml:"Type"`
	Default string            `yaml:"Default"`
	Props   map[string]string `yaml:"Props"`
	Values  []string          `yaml:"Values"`
	Rules   []ConfigRule      `yaml:"Rules"`
}

The YAML config struct for a field

type ConfigRule

type ConfigRule struct {
	Type   string `yaml:"Type"`
	Err    string `yaml:"Err"`
	Regex  string `yaml:"Regex"`
	Length int    `yaml:"Length"`
}

The validation struct

type Field

type Field struct {
	// The field name
	Name string
	// The field label
	Label string
	// The field type ie text, textarea
	Type string
	// The default value
	Default string
	// The actual value
	Value string
	// Boolean flag for when a field has errors or not
	Valid bool
	// A list of values to use for multi-value inputs
	Values []string
	// A list of validation items to add to this field
	Rules []Validation
	// A list of errors on this field
	Errors []string
	// A Map of properties to add to the field output
	Props map[string]string
}

func NewField

func NewField() *Field

Creates a new field struct

type Form

type Form struct {
	// The form name
	Name string
	// The form description
	Description string
	// The form action
	Action string
	// The HTTP Method
	Method string
	// A list of fields
	Fields []*Field
	// The current request
	Request http.Request
	// Html Property map
	Props map[string]string
	// Boolean flag whether form is valid or not
	Valid bool
}

func NewForm

func NewForm(r http.Request) *Form

Creates the form struct

type LengthValidation

type LengthValidation struct {
	Value string
	Err   string
	Valid bool
	Len   int
}

Checks for a specific length

type RegexValidation

type RegexValidation struct {
	Value string
	Err   string
	Valid bool
	Regex string
}

Custom Regex validation

type Required

type Required struct {
	Value string
	Err   string
	Valid bool
}

Required Checks whether any value is set

type Validation

type Validation interface {
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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