goform

package module
v0.0.0-...-9a8cdb4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2023 License: MPL-2.0 Imports: 7 Imported by: 0

README

goform

goform generate HTML forms dynamically and super easy using Golang/Go.

Status

In development V2.0

Description

goform is a super simple form generator, create dynamic forms without having to write HTML.

Templates included:

  • bootstrap5
  • html

By default goform render forms in Bootstrap 5 style, also its posible to choose html format, where the inputs are render in plain html (no divs, no labels, ...)

If anyone need a custom template or custom items, goform has the option to choose custom template.

TODO:

  • Parse HTML in textlabel element
  • Remove Elements Functions
  • Enable Set/Group of elements

Example

package main

import(
    "text/template"

    "github.com/irob/goform"
)

var res = make(map[string]interface{})
var tmpl = template.Must(template.ParseGlob("tmpl/*"))

func main () {

    nInputs := 8

    // CitiesList slice of cities
    var CitiesList = []OptionItem{{Key: "", Value: "Choose your favorite city"}, {Key: "AMS", Value: "Amsterdam"}, {Key: "VEN", Value: "Venice"}, {Key: "KYO", Value: "Kyoto"}, {Key: "PAR", Value: "Paris"}, {Key: "DOH", Value: "Doha"}, {Key: "BAR", Value: "Barcelona"}, {Key: "SMA", Value: "San Miguel de Allende"}, {Key: "BUD", Value: "Budapest"}, {Key: "LIS", Value: "Lisbon"}, {Key: "FLO", Value: "Florence"}, {Key: "HNK", Value: "Hong Kong"}, {Key: "BRU", Value: "Bruges"}}
    // AgeRanges slice of ranges of ages
    var AgeRanges = []OptionItem{{Key: "1", Value: "1 - 9 yo"}, {Key: "2", Value: "10 - 19 yo"}, {Key: "3", Value: "20 - 29 yo"}, {Key: "4", Value: "30 - 39 yo"}, {Key: "5", Value: "40 - 49 yo"}, {Key: "6", Value: ">= 50 yo"}}

    form := Create("profile_form", "POST", "/goform")

    form.DefaultGroupClass("col-md-12")
    form.DefaultGroupClass("mb-2")

    // Label input
    form.NewElement("label", "userdetails", "User profile")

    // Text input
    form.NewElement("text", "text", "")
    form.SetLabel("text", "What's your name")

    // Textlabel input
    form.NewElement("textlabel", "username", "john@bender.com")
    form.SetLabel("username", "Your username:")

    // Password input
    form.NewElement("password", "password", "")

    // Select input
    form.NewElement("select", "select", "VEN")
    form.SetOptions("select", CitiesList)

    // Radio input
    form.NewElement("radio", "radio", "")
    form.SetLabel("radio", "Age range")
    form.SetOptions("radio", AgeRanges)

    // Textarea
    form.NewElement("textarea", "textarea", "")
    form.SetHelpText("textarea", "Error, must write a resume description")

    // Checkbox
    form.NewElement("checkbox", "checkbox", "")

    // File input
    form.NewElement("file", "file", "")

    // Hidden
    form.NewElement("hidden", "hidden", "")

    // Full address init
    form.NewElement("label", "address_info", "Full address")
    form.AddGroupClass("address_info", "col-md-2")
    form.AddGroupClass("address_info", "mb-2")

    form.NewElement("text", "street", "")
    form.SetPlaceHolder("street", "Street")
    form.AddParams("street", "maxlength", "20")
    form.AddGroupClass("street", "col-md-4")
    form.AddGroupClass("street", "mb-2")

    form.NewElement("text", "number", "")
    form.SetPlaceHolder("number", "Number")
    form.AddParams("number", "maxlength", "20")
    form.AddGroupClass("number", "col-md-2")
    form.AddGroupClass("number", "mb-2")

    form.NewElement("select", "city", "VEN")
    form.SetOptions("city", CitiesList)
    form.AddGroupClass("city", "col-md-4")
    form.AddGroupClass("city", "mb-2")
    // Full address end

    form.NewRow("skills")
    // Dyanmic inputs
    for i := 1; i <= nInputs; i++ {
        form.NewElement("text", "skill_"+strconv.Itoa(i), "")
        form.SetPlaceHolder("skill_"+strconv.Itoa(i), "Skill "+strconv.Itoa(i))
        form.AddGroupClass("skill_"+strconv.Itoa(i), "col-md-6")
        form.AddGroupClass("skill_"+strconv.Itoa(i), "mb-2")
    }

    // Buttons
    form.NewElement("submit", "submit", "Update profile")
    form.AddClass("submit", "btn-success")
    form.AddClass("submit", "btn-lg")
    form.AddClass("submit", "btn-xl")
    form.AddClass("submit", "btn-block")
    form.AddClass("submit", "w-100")

    form.NewElement("button", "button", "Cancel edit profile")
    form.AddClass("button", "btn-secondary")
    form.AddClass("button", "btn-lg")
    form.AddClass("button", "btn-xl")

    // Send to template
    res["Form"] = form

    tmpl.ExecuteTemplate(w, "HTMLTemplate", res)
}

In your HTML template place {{ .Form.Render }}

Real demo

https://robertomo.com/goform

Requests or bugs?

https://github.com/irob/goform/issues

Installation

go get github.com/irob/goform

Use you custom templates

/templates/custom_templates is a templete based on Bootstrap5.

Step 1.-
Move/Copy the folder /templates into your application, rename/copy the subdirectory /custom_templates.

Step 2.-
Customize the .html files of the elements you want with your own HTML format.

Step 3.-
Change the form style template name to you new template form.SetOwnStyleTemplate(YOUR_CUSTOM_THEME_NAME).

License

The source files are distributed under the Mozilla Public License, version 2.0, unless otherwise noted. Please read the FAQ if you have further questions regarding the license.

Documentation

Overview

Package goform Generate html forms dynamically and super simple using Golang/Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTMLTemplate

func HTMLTemplate(theme string, input string) *template.Template

HTMLTemplate parse html template

func LogOutput

func LogOutput(format string) string

LogOutput display the Log

Types

type Element

type Element struct {
	ID   int
	Name string
}

Element structure.

type ErrorItem

type ErrorItem struct {
	RelatedTo string
	Message   string
}

type Field

type Field struct {
	Position    int
	FieldType   string
	Name        string
	ID          string
	Classes     []string
	CSS         map[string]string
	Label       string
	LabelClass  []string
	Value       string
	Options     []OptionItem
	PlaceHolder string
	HelpText    string
	Params      map[string]string
	Set         string
	GroupClass  []string
}

Field structure.

func EmptyField

func EmptyField() Field

EmptyField create and return empty form field

type FieldIndex

type FieldIndex struct {
	Index int
	Field Field
}

type Form

type Form struct {
	Name              string
	ID                string
	Method            string
	Action            string
	MultipartFormData string
	TemplateStyle     string
	TemplateSource    string
	FormTypes         map[string]int
	Elements          map[string]Field
	Classes           []string
	CSS               map[string]string
	FormText          string
	FormTemplates     map[string]*template.Template
	GroupClass        []string
}

Form structure.

func Create

func Create(name string, method string, action string) *Form

Create configure a new Form structure

func (*Form) AddCSS

func (f *Form) AddCSS(fieldName string, key, value string)

AddCSS add a CSS value (in the form of option-value - e.g.: color - red).

func (*Form) AddClass

func (f *Form) AddClass(fieldName string, class string)

AddClass adds a class to the input.

func (*Form) AddGroupClass

func (f *Form) AddGroupClass(fieldName string, class string)

AddGroupClass adds a class to the group input.

func (*Form) AddLabelClass

func (f *Form) AddLabelClass(fieldName string, class string)

AddLabelClass adds a class to the label of the input.

func (*Form) AddParams

func (f *Form) AddParams(fieldName string, key, value string)

AddParams add a Param value (in the form of option-value - e.g.: maxlength - 15).

func (*Form) DefaultGroupClass

func (f *Form) DefaultGroupClass(width string)

DefaultGroupClass set default group classes for all the elements

func (*Form) NewButton

func (f *Form) NewButton(buttonName string)

NewButton insert a new button shortcut.

func (*Form) NewElement

func (f *Form) NewElement(fieldType string, fieldName string, fieldValue string) string

NewElement insert new form element

func (*Form) NewRow

func (f *Form) NewRow(rowName string)

NewRow insert a new row shortcut.

func (*Form) Render

func (f *Form) Render() template.HTML

Render returns form generated in plain format

func (*Form) RenderElements

func (f *Form) RenderElements() template.HTML

RenderElements returns form elements generated in plain format

func (*Form) SetHelpText

func (f *Form) SetHelpText(fieldName string, helptext string)

SetHelpText set the help-text to the input.

func (*Form) SetID

func (f *Form) SetID(fieldName string, id string)

SetID set/change the ID to the field.

func (*Form) SetLabel

func (f *Form) SetLabel(fieldName string, label string)

SetLabel set/change the text label to the field.

func (*Form) SetMultipartFormData

func (f *Form) SetMultipartFormData(status string)

SetMultipartFormData set style format, (html or bootstrap5: default option)

func (*Form) SetOptions

func (f *Form) SetOptions(fieldName string, options []OptionItem)

SetOptions set/change the Options of the dropdown.

func (*Form) SetOwnTemplateStyle

func (f *Form) SetOwnTemplateStyle(style string)

SetOwnTemplateStyle set style format, target different templates folder In case if any one need to use a custom templates

func (*Form) SetPlaceHolder

func (f *Form) SetPlaceHolder(fieldName string, placeholder string)

SetPlaceHolder set the placeholder text to the input.

func (*Form) SetTemplateStyle

func (f *Form) SetTemplateStyle(style string)

SetTemplateStyle set style format, (html or bootstrap5: default option)

func (*Form) SortElements

func (f *Form) SortElements() []Field

SortElements create and return empty form field

type OptionItem

type OptionItem struct {
	Key   string
	Value string
}

OptionItem structure.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL