forms

package module
v0.0.1-rc.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2017 License: MIT Imports: 2 Imported by: 0

README

gothite/forms

Form handling and data validation for Go

Installation

go get github.com/gothite/forms

Usage

package main

import (
        "fmt"

        "github.com/gothite/forms"
        "github.com/gothite/forms/fields"
        "github.com/gothite/forms/validators"
)

func authorize(form *forms.Form) error {
        // check user data
        // form.Fields["email"].Value
        // form.Fields["password"].Value
        return nil
}

// LoginForm handles user login.
var LoginForm = forms.NewFormFactory(
        authorize, // or nil
        &fields.Email{
                Name:   "email",
                Errors: map[string]string{"Invalid": "Please, set a valid email."},
        },
        &fields.String{Name: "password", MinLength: validators.MinLength(5)},
)

func main() {
        data := map[string]interface{}{"email": "me@pyvimcom", "password": "pass"}
        form := LoginForm(data)

        if !form.IsValid() {
                fmt.Printf("Form error: %v\n", form.Error)

                for name, field := range form.Fields {
                        fmt.Printf("%v error: %v\n", name, field.Error)
                }
        }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldWrapper

type FieldWrapper struct {
	fields.Field

	Value interface{}
	Error error
}

FieldWrapper wraps Field instance to keep current form value and possible error.

func (*FieldWrapper) Validate

func (wrapper *FieldWrapper) Validate(v interface{}) (interface{}, error)

Validate checks an input value using Field.Validate and field validators.

type Form

type Form struct {
	Fields map[string]*FieldWrapper
	Data   map[string]interface{}
	Error  error
	Clean  func(form *Form) error
}

Form describes a form validator.

func (*Form) IsValid

func (form *Form) IsValid() bool

IsValid checks data and returns true if data is valid.

type FormFactory

type FormFactory func(map[string]interface{}) *Form

FormFactory implements form factory.

func NewFormFactory

func NewFormFactory(clean func(form *Form) error, fields ...fields.Field) FormFactory

NewFormFactory initializes factory to make forms with specified fields.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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