FAQ
I've building web apps in go for the past year and half. I built this
library to make it easier to parse form data. It supports multipart forms,
url-encoded forms, url query parameters, and json. It also supports a good
number of validations. Source code and guides are on github
<https://github.com/albrow/forms>. Feedback welcome :)

Here's example usage:

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



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 24, '15 at 12:07a
activeFeb 24, '15 at 12:07a
posts1
users1
websitegolang.org

1 user in discussion

Alex Browne: 1 post

People

Translate

site design / logo © 2022 Grokbase