Coder Social home page Coder Social logo

goforms's Introduction

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.

goforms's People

Contributors

absoludity avatar martinbrugnara avatar temoto avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.