Skip to content

absoludity/goforms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoForms - form data validation, cleaning and error reporting

The goforms library is a proof-of-concept for a data validation, cleaning and error collecting using Go, in a similar style to Django's django.forms library. It enables thin handlers like this:

func my_post_handler(w http.ResponseWriter, r *http.Request) {
    // Fill r.Form
    if err := r.ParseForm(); err != nil {
        http.Error(w, "Invalid request form data", 400)
        return
    }    
    myForm := MyCustomForm()
    myForm.Data = r.Form

    if myForm.IsValid() {
        // Do something with myForm.CleanedData, which is the request's form
        // data after being cleaned to the correct types etc.
    } else {
        // Re-render a template, passing in myForm.Errors which is a map
        // of errors for each field on the form.
    }

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

Installation and tests

To install goforms into your current Go workspace:

$ go get github.com/absoludity/goforms/forms

You can then run the tests with

$ go test github.com/absoludity/goforms/fields github.com/absoludity/goforms/forms

Example

Define your form

	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}

Use the form in your handler

Using the http.Request objects Form (r.Form):

	personForm.Data = r.Form

	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."
		// }
	}

Notes

TODO

  • Remove Form.Data, and instead provide data to Form.IsValid()
  • Perhaps make Form.CleanedData and Form.Errors private, providing getters.
  • Add defaults to fields so that a value is always included.
  • Enable custom error messages.
  • Update field tests to use nicer in/out for test tables like form_tests.

License

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.

About

Data validation, cleaning and error collection for golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages