• Stars
    star
    137
  • Rank 266,121 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A lightweight go library for parsing form data or json from an http.Request.

Forms

GoDoc

Forms is a lightweight, but incredibly useful go library for parsing form data from an http.Request. It supports multipart forms, url-encoded forms, json data, and url query parameters. It also provides helper methods for converting data into other types and a Validator object which can be used to validate the data. Forms is framework-agnostic and works directly with the http package.

Version 0.4.0

Development Status

Forms is no longer actively maintained, and therefore it is not recommended for use in mission-critical production applications at this time. That said, it is fairly well tested and is probably fine to use for low-traffic hobby sites.

Forms follows semantic versioning but offers no guarantees of backwards compatibility until version 1.0. Keep in mind that breaking changes might occur. We will do our best to make the community aware of any non-trivial breaking changes beforehand. We recommend using a dependency vendoring tool such as godep to ensure that breaking changes will not break your application.

Installation

Install like you would any other package:

go get github.com/albrow/forms

Then include the package in your import statements:

import "github.com/albrow/forms"

Example Usage

Meant to be used inside the body of an http.HandlerFunc or any function that has access to an http.Request.

func CreateUserHandler(res http.ResponseWriter, req *http.Request) {
	// Parse request data.
	userData, err := forms.Parse(req)
	if err != nil {
		// Handle err
		// ...
	}

	// Validate
	val := userData.Validator()
	val.Require("username")
	val.LengthRange("username", 4, 16)
	val.Require("email")
	val.MatchEmail("email")
	val.Require("password")
	val.MinLength("password", 8)
	val.Require("confirmPassword")
	val.Equal("password", "confirmPassword")
	val.RequireFile("profileImage")
	val.AcceptFileExts("profileImage", "jpg", "png", "gif")
	if val.HasErrors() {
		// Write the errors to the response
		// Maybe this means formatting the errors as json
		// or re-rendering the form with an error message
		// ...
	}

	// Use data to create a user object
	user := &models.User {
		Username: userData.Get("username"),
		Email: userData.Get("email"),
		HashedPassword: hash(userData.Get("password")),
	}

	// Continue by saving the user to the database and writing
	// to the response
	// ...


	// Get the contents of the profileImage file
	imageBytes, err := userData.GetFileBytes("profileImage")
	if err != nil {
	  // Handle err
	}
	// Now you can either copy the file over to your server using io.Copy,
	// upload the file to something like amazon S3, or do whatever you want
	// with it.
}

Contributing

See CONTRIBUTING.md

License

Forms is licensed under the MIT License. See the LICENSE file for more information.

More Repositories

1

fo

An experimental language which adds functional programming features to Go.
Go
1,237
star
2

jobs

A persistent and flexible background jobs library for go.
Go
494
star
3

zoom

A blazing-fast datastore and querying engine for Go built on Redis.
Go
309
star
4

vdom

A virtual dom implementation written in go which is compatible with gopherjs
Go
90
star
5

elara

Work-in-progress educational programming game
TypeScript
71
star
6

people

A simple HTTP/JSON API written in Go using the Negroni middleware library and Zoom as a datastore.
Go
13
star
7

negroni-json-recovery

Middleware for negroni which catches panics and wraps them into a JSON response.
Go
11
star
8

liquid_time_ago_in_words

A rails-style time_ago_in_words filter for the liquid templating language
Ruby
8
star
9

stringset

A simple and space-effecient Go implementation of a set of strings.
Go
6
star
10

blog-old

The old version of my blog, built with jekyll + octopress
Ruby
6
star
11

fipple

A testing utility for go which lets you easily record and test http responses from any url
Go
4
star
12

doc_parser

A docx parser written in Ruby
Ruby
4
star
13

scribble

A tiny static blog generator written in go.
Go
3
star
14

jekyll-bootstrap-boilerplate

A boilerplate jekyll setup that includes a sass port of bootstrap. I use this as a starting point when designing static websites.
Ruby
3
star
15

prtty

A small go library for logging things with color.
Go
2
star
16

gopherjs-router

A client-side router written in go that compiles to javascript via gopherjs
JavaScript
2
star
17

confirm

A tiny but useful utility for confirming actions, typically used in user-executable scripts or makefiles.
Go
2
star
18

editor

A minimal command line text editor written in go
Go
2
star
19

todo-backend

A simple REST backend for a todoMVC application. Written in go.
Go
1
star
20

timelord

A Go library which allows manipulating time by monkey-patching time.Now
Go
1
star
21

gopherjs-watch

A demo/prototype of two-way data binding with gopherjs
JavaScript
1
star
22

gopherchat

A peer-to-peer command line chat application built using golang. For science!
Go
1
star
23

bullhorn

An easy-to-use SMS and voice broadcast system. You can use it to keep clients who might not have internet access up-to-date.
Go
1
star
24

wemo

Go
1
star
25

go-benchmark-example

An example of how to benchmark and optimize go programs, in particular computing binomial coefficients.
Go
1
star
26

go-linkedlist

A sorted, doubly-linked list implementation written in Go
Go
1
star
27

sids

Bracket website for fundraising for SIDS
Ruby
1
star
28

sample_app

Ruby on Rails Tutorial sample application
Ruby
1
star
29

dependency-linearization

An experiment in finding a fast dependency linearization algorithm for go.
Go
1
star
30

gqlgen-todos

An example of using gqlgen to generate a simple GraphQL API
Go
1
star
31

zoom_example

An example of how to use my go+redis ORM library called zoom
Go
1
star
32

gopherjs-live

Automatic watching and recompiling for gopherjs
Go
1
star
33

calc

A simple command-line calculator program written in go. Good practice for interpreters/compilers.
Go
1
star
34

Pedia

A simple command line tool for querying wikipedia using bash and dig.
Shell
1
star
35

golearn-digit-recognition

Handwritten digit recognition in go using golearn
Go
1
star
36

demo_app

Ruby on Rails Tutorial demo application
Ruby
1
star
37

martini-json-recovery

Middleware for martini which catches panics and wraps them into a JSON response.
Go
1
star
38

xml_format

A simple command line tool for auto-formatting xml documents. Written in Ruby, and uses the ultra-fast libxml-ruby gem.
Ruby
1
star
39

first_app

The first app created for the Ruby on Rails Tutorial
Ruby
1
star