HTML forms for Golang

HTML forms for Golang

Installation:

go get github.com/vmihailenco/gforms

Example

Example:

package blog

import (
    "net/http"

    "github.com/vmihailenco/gforms"
)

type ArticleForm struct {
    gforms.BaseForm
    Title    *gforms.StringField `gforms:",req"`
    Text     *gforms.StringField `gforms:",req"`
    IsPublic *gforms.BoolField
}

func NewArticleForm(article *Article) *ArticleForm {
    f := &ArticleForm{}
    gforms.InitForm(f)

    f.Title.MaxLen = 500
    f.IsPublic.Label = "Is public?"

    if article != nil {
        f.Title.SetInitial(article.Title)
        f.Text.SetInitial(article.Text())
        f.IsPublic.SetInitial(article.IsPublic)
    }

    return f
}

func (f *ArticleForm) Populate(article *Article) {
    article.Title = f.Title.Value()
    article.Text = f.Text.Value()
    article.IsPublic = f.IsPublic.Value()
}

func CreateArticleHandler(w http.ResponseWriter, r *http.Request) {
    form := NewArticleForm(nil)

    if r.Method == "POST" {
        _ = r.ParseForm()
        if gforms.IsFormValid(form, r.Form) {
            article := &Article{}
            form.PopulateArticle(article)

            if err := SaveArticle(article); err != nil {
                HandleError(w, err)
                return
            }

            http.Redirect(w, r, "/articles", http.StatusFound)
            return
        }
    }

    data := struct {
        Form *ArticleForm
    } {
        Form: form,
    }
    RenderTemplate(w, data)
}

Template:

<form method="post" class="well article">
  {{render .Form.Title "class" "span6"}}
  {{render .Form.Text "class" "span6"}}
  {{render .Form.IsPublic}}

  <div class="form-actions">
    <button type="submit" class="btn btn-primary">Create New Article</button>
  </div>
</form>
Owner
Similar Resources

Go-httpheaders - Canonicalized forms of common HTTP headers for Go

Go-httpheaders - Canonicalized forms of common HTTP headers for Go

Mar 20, 2022

Simple system for writing HTML/XML as Go code. Better-performing replacement for html/template and text/template

Simple system for writing HTML as Go code. Use normal Go conditionals, loops and functions. Benefit from typing and code analysis. Better performance than templating. Tiny and dependency-free.

Dec 5, 2022

VMail - check the markup (HTML, CSS) of HTML email template compatibility with email clients

VMail - check the markup (HTML, CSS) of HTML email template compatibility with email clients

VMail - check the markup (HTML, CSS) of HTML email template compatibility with email clients Email clients use different rendering standards. This is

Dec 17, 2022

This command line converts .html file into .html with images embed.

embed-html This command line converts .html file into .html with images embed. Install go get github.com/gonejack/embed-html Usage embed-html *.ht

Oct 6, 2022

Inline styling for html mail in golang

go-premailer Inline styling for HTML mail in golang Document install go get github.com/vanng822/go-premailer/premailer Example import ( "fmt" "gith

Nov 30, 2022

Golang package that generates clean, responsive HTML e-mails for sending transactional mail

Golang package that generates clean, responsive HTML e-mails for sending transactional mail

Hermes Hermes is the Go port of the great mailgen engine for Node.js. Check their work, it's awesome! It's a package that generates clean, responsive

Dec 28, 2022

Golang bindings of Sciter: the Embeddable HTML/CSS/script engine for modern UI development

Golang bindings of Sciter: the Embeddable HTML/CSS/script engine for modern UI development

Go bindings for Sciter Check this page for other language bindings (Delphi / D / Go / .NET / Python / Rust). Attention The ownership of project is tra

Dec 23, 2022

Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.

goview Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application. Contents Inst

Dec 25, 2022

htmlquery is golang XPath package for HTML query.

htmlquery Overview htmlquery is an XPath query package for HTML, lets you extract data or evaluate from HTML documents by an XPath expression. htmlque

Jan 4, 2023

Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and struct tags for golang crawler

Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and struct tags for golang crawler

Pagser Pagser inspired by page parser。 Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and str

Dec 13, 2022

XPath package for Golang, supports HTML, XML, JSON document query.

XPath XPath is Go package provides selecting nodes from XML, HTML or other documents using XPath expression. Implementation htmlquery - an XPath query

Dec 28, 2022

Golang HTML to plaintext conversion library

html2text Converts HTML into text of the markdown-flavored variety Introduction Ensure your emails are readable by all! Turns HTML into raw text, usef

Dec 28, 2022

Composable HTML components in Golang

Composable HTML components in Golang

daz Composable HTML components in Golang Daz is a "functional" alternative to using templates, and allows for nested components/lists Also enables tem

Oct 3, 2022

Golang HTML to PDF Converter

Golang HTML to PDF Converter

Golang HTML to PDF Converter For reading any document, one prefers PDF format over any other formats as it is considered as a standard format for any

Dec 15, 2022

Frongo is a Golang package to create HTML/CSS components using only the Go language.

Frongo Frongo is a Go tool to make HTML/CSS document out of Golang code. It was designed with readability and usability in mind, so HTML objects are c

Jul 29, 2021

⚙️ Concept of Golang HTML render engine with frontend components and dynamic behavior

⚙️ Concept of Golang HTML render engine with frontend components and dynamic behavior

An HTML render engine concept that brings frontend-like components experience to the server side with native html/template on steroids. Supports any s

Nov 25, 2022

⚙️ Concept of Golang HTML render engine with frontend components and dynamic behavior

SSC Engine An HTML render engine concept that brings frontend-like components experience to the server side with native html/template on steroids. Sup

Nov 25, 2022

golang program that simpily converts html into markdown

Simpily converts html to markdown Just a simple project I wrote in golang to convert html to markdown, surprisingly works decent for a lot of websites

Oct 23, 2021

yview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.

wview wview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application. Contents Instal

Dec 5, 2021
Comments
  • * Change RenderField to use variadic parameter, so it can be called from...

    * Change RenderField to use variadic parameter, so it can be called from...

    ... a template using only one parameter

    • Allows manual field error setting to BaseForm (for exemple, to show API errors)
    • Fields retain the entered value even on error
Related tags
Golang Forms made easy.

Go-FORM-it Description go-form-it makes form creation and handling easy. It allows the creation of form without having to write HTML code or bother to

Aug 20, 2022
Trims, sanitizes & scrubs data based on struct tags (go, golang)

Conform- keep user input in check (go, golang) Trim, sanitize, and modify struct string fields in place, based on tags. Update Jan 12, 2016 -- Now als

Dec 20, 2022
Data validation, cleaning and error collection for golang

GoForms - form data validation, cleaning and error reporting The goforms library is a proof-of-concept for a data validation, cleaning and error colle

Jan 14, 2022
CSRF prevention for the Golang Revel framework.

revel-csrf revel-csrf implements Cross-Site Request Forgery (CSRF) attacks prevention for the Revel framework. Code is based on the nosurf package imp

Dec 7, 2022
Go library for parsing and submitting HTML forms

gosubmit Description Docs are available here: https://godoc.org/github.com/jeremija/gosubmit Helps filling out plain html forms during testing. Will a

Nov 14, 2022
Golang Forms made easy.

Go-FORM-it Description go-form-it makes form creation and handling easy. It allows the creation of form without having to write HTML code or bother to

Aug 20, 2022
Forms is a fast, powerful, flexible, sortable web form rendering library written in golang.

forms Description forms makes form creation and handling easy. It allows the creation of form without having to write HTML code or bother to make the

Oct 2, 2022
bluemonday: a fast golang HTML sanitizer (inspired by the OWASP Java HTML Sanitizer) to scrub user generated content of XSS

bluemonday bluemonday is a HTML sanitizer implemented in Go. It is fast and highly configurable. bluemonday takes untrusted user generated content as

Jan 4, 2023
It's so many regular expression forms are difficult to understand, such as perl, python, grep awk

Introduction Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. It

Mar 31, 2022
DeepCopy a portable app that allows you to copy all forms of specified file types from your entire file system of the computer

DeepCopy a portable app that allows you to copy all forms of specified file types from your entire file system of the computer

Dec 20, 2021