forms

package
v0.0.0-...-c4682c2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2014 License: BSD-3-Clause Imports: 2 Imported by: 2

Documentation

Overview

Copyright 2012 The GoForms Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Extended by MartinBrugnara (2014) <martin@martin-dev.eu>

Package goforms/forms enables form data validation, cleaning and error collection, similar to Django forms. From forms_test.go:

formFields := FormFields{
	"name": fields.NewCharField(fields.Defaults{
		"Required": true,
	}),
	"age": fields.NewIntegerField(fields.Defaults{
		"Required": false,
	}),
	"about": fields.NewCharField(fields.Defaults{
		"Max": 10,
	}),
}
personForm := Form{Fields: formFields}

personForm.Data = urls.Values{
	"name": {"Michael Nelson"},
	"age":  {"37"},
}

if personForm.IsValid() {
	doStuffWithCleanedData()
	// personForm.CleanedData contains cleaned data
	// (ie. an int for age in this case):
	// {"name": "Michael Nelson", "age": 37}
	// so that for required fields you can safely do:
	// var age int = personForm.CleanedData["age"].(int)
} else {
	doStuffWithErrors()
	// If personForm.Data = urls.Values{"age": {"Not a number"}},
	// then personForm.Errors would be {
	//	 "name": "This field is required.",
	//	 "age":  "The value ust be a valid integer."
	// }
}

You can see more examples in goforms/forms/forms_test.go.

For another form-data processing library, see github.com/gorilla/schema, which fills structs with form data using struct tags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanedData

type CleanedData map[string]interface{}

Contains the cleaned data after a call to IsValid().

type Form

type Form struct {
	Fields      FormFields
	Data        url.Values
	CleanedData map[string]interface{}
	Errors      map[string]string
}

A form brings together a collection of fields, the form data to be validated against those fields, as well as the generated cleaned data or the collection of errors.

func (*Form) IsValid

func (f *Form) IsValid() bool

Returns whether the given data validates against the form's fields. If successful, CleanedData will be populated, otherwise Errors will be populated.

type FormFields

type FormFields map[string]fields.Field

A collection of fields used on a Form.

Jump to

Keyboard shortcuts

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