form

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2017 License: LGPL-2.1 Imports: 32 Imported by: 2

README

Go-Form

Create HTML forms with your structs and interfaces.

Requirements
Install and import

Add these tags within your .gxml file.

<import src="github.com/cheikhshift/form/gos.gxml" />	
<import src='netform "github.com/cheikhshift/form"' />	
Configure

Set max upload size :

netform.MaxSize = 20 //mb

Set form token key :

netform.FormKey = "a very very very very secret key"

Set input field class :

netform.InputClass = ""

Set submit button class :

netform.ButtonClass = ""
How to use with AngularJS

Build a new form compatible with Angular using the following template pipeline :

{{ AngularForm $arg1 $arg2 $arg3 $arg4 $arg5 .Session }}

Argument information :

  • $arg1 : Interface{} - Interface to build form with. Submit a variable with data to prepopulate form.
  • $arg2 : String - Target URL to submit form to. This is used to generate a token only valid for the specified target URL path.
  • $arg3 : String - JS Function to use with form's submit ng-click .
  • $arg4 : Call to action of form button.
  • $arg5 : String - variable name to be used as a local scope object to hold form data.
  • .Session : *sessions.Session - Current user session. Must be passed to ensure secure communication.
How to use

Build a new form within a Golang server template:

{{ Build $arg1 $arg2 $arg3 $arg4 .Session }}

Argument information :

  • $arg1 : Interface{} - Interface to build form with. Submit a variable with data to prepopulate form.
  • $arg2 : String - Target URL to submit form to.
  • $arg3 : String - Method of form submission (GET,POST,PUT etc...).
  • $arg4 : String - Call to action of form button.
  • .Session : *sessions.Session Current user session. Must be passed to ensure secure communication.

Build within <end> tag :

net_Build(param1 interface{}, param2 string, param3 string, param4 string, param5 *sessions.Session) string

Parameter information :

  • param1 : Interface to build form with. Submit a variable with data to prepopulate form.
  • param2 : Target URL to submit form to.
  • param3 : Method of form submission.
  • param4 : Call to action of form button.
  • param5 : Current user session. Must be passed to ensure secure communication.
Server side validation

Please visit the GoValidator page for valuable tag information. here

How to process data.

Within your <end> tag use the following function to validate and convert the post body to the specified interface.

var sampleform SampleForm
err := netform.Form(r, &sampleform)
Field types

List of field types with associated tag behavior.

1. string

Display text input box.

Tag properties :

  • title : title of field.
  • placeholder : placeholder of field.
2. int | float (any number)

Display number input box.

Tag properties :

  • title : title of field.
  • placeholder : placeholder of field.
3. bool

Display Checkbox.

Tag properties :

  • title : text blurb right of checkbox.
4. File

Display file upload box. Use this field property with function netform.Pathto get local filesystem path.

Tag properties :

  • title : title of field.
  • file : Mimetype of file to upload.
5. Paragraph

Display text area.

Tag properties :

  • title : title of field.
  • placeholder : placeholder of field.
6. Date

Display date input.

Tag properties :

  • title : title of field.
  • placeholder : placeholder of field.
7. Select

Display dropdown list.

Tag properties :

  • title : title of field.
  • placeholder : Prompt left of field.
  • select : comma delimited choices of field.
8. SelectMult

Display dropdown list with multiple selection support.

Tag properties :

  • title : title of field.
  • placeholder : Prompt left of field.
  • select : comma delimited choices of field.
9. Radio

Display radio input.

Tag properties :

  • title : title of field.
  • select : comma delimited choices of field.
10. Email

Display email input.

Tag properties :

  • title : title of field.
  • placeholder : placeholder of field.
11. Password

Display password input.

Tag properties :

  • title : title of field.
  • placeholder : placeholder of field.
Samples

Sample of GoS <struct/> with form tags set :

<struct name="SampleForm">
		TestField string `title:"Hi world!",valid:"unique",placeholder:"Testfield prompt"`
		Count int `placeholder:"Count"`
		Name string `valid:"required",title:"Input title"`
		FieldTwo netform.Radio `title:"Enter Email",valid:"email,unique,required",select:"blue,orange,red,green"`
		FieldF netform.Select `placeholder:"Prompt?",valid:"email,unique,required",select:"blue,orange,red,green"`
		Created netform.Date
		Text netform.Paragraph 	`title:"Enter a description."`
		Photo netform.File 	`file:"image/*"`
		Emal netform.Email
		Terms bool	`title:"Accept terms of use."`
</struct>

Documentation

Index

Constants

View Source
const (
	MB = 1 << 20
)

select opts,placeholder,title

Variables

View Source
var (
	// string : Default input classes of
	// generared HTML input class attribute.
	InputClass string
	// string : Default submit button attribute class of
	// generated HTML forms.
	ButtonClass string
	// string : Secret form key
	FormKey = "a very very very very secret key"
	// int : Maximum upload size in Mb
	MaxSize = 10 //Mb
)

Functions

func Asset added in v0.2.1

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir added in v0.2.1

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo added in v0.2.1

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames added in v0.2.1

func AssetNames() []string

AssetNames returns the names of the assets.

func BytesToString added in v0.2.1

func BytesToString(b []byte) string

func DebugTemplate added in v0.2.1

func DebugTemplate(w http.ResponseWriter, r *http.Request, tmpl string)

func DebugTemplatePath added in v0.2.1

func DebugTemplatePath(tmpl string, intrf interface{})

func Decrypt

func Decrypt(key []byte, cryptoText string) string

decrypt from base64 to decrypted string

func Encrypt

func Encrypt(key []byte, text string) string

func FileServer added in v0.2.1

func FileServer() http.Handler

func Form

func Form(r *http.Request, i interface{}) error

Parse form from request and set data to specified pointer.

func GenerateToken

func GenerateToken(url string, usertoken string) string

Returns form token. Each token is specific to the URL specified.

func GetLine added in v0.2.1

func GetLine(fname string, match string) int

func GetPl

func GetPl(tagstr string) string

func GetSel

func GetSel(tagstr string) string

func Handler added in v0.2.1

func Handler(w http.ResponseWriter, r *http.Request)

func Hash added in v0.2.1

func Hash(input string) []byte

func MakeHandler added in v0.2.1

func MakeHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc

Access you .gxml's end tags with this http.HandlerFunc. Use MakeHandler(http.HandlerFunc) to serve your web directory from memory.

func MustAsset added in v0.2.1

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func NetAngularForm added in v0.2.1

func NetAngularForm(args ...interface{}) string

Return string of Angular form Argument 1 : Interface{} - Interface to build form with. Submit a variable with data to prepopulate form. Argument 2 : String - Target URL to submit form to. This is used to generate a token only valid for the specified target URL path. Argument 3 : String - JS Function to use with form's submit ng-click . Argument 4 : Call to action of form button. Argument 5 : String - variable name to be used as a local scope object to hold form data. Argument 5 : *sessions.Session Current user session. Must be passed to ensure secure communication.

func NetBuild added in v0.2.1

func NetBuild(args ...interface{}) string

Return HTML form Argument 1 : Interface{} - Interface to build form with. Submit a variable with data to prepopulate form. Argument 2 : String - Target URL to submit form to. Argument 3 : String - Method of form submission (GET,POST,PUT etc...). Argument 4 : String - Call to action of form button. Argument 5 : *sessions.Session Current user session. Must be passed to ensure secure communication.

func NetForm added in v0.2.1

func NetForm(args ...interface{}) string

func NetGenerateToken added in v0.2.1

func NetGenerateToken(args ...interface{}) string

Return string of form token. Argument 0 : String of URI your form is submitting to Argument 1 : *sessions.Session (github.com/gorilla/sessions)

func NetHasBody added in v0.2.1

func NetHasBody(args ...interface{}) bool

func NetIsIn added in v0.2.1

func NetIsIn(args ...interface{}) bool

func NetTokenizeForm added in v0.2.1

func NetTokenizeForm(args ...interface{}) (form fForm)

func NetTokenizeFormAng added in v0.2.1

func NetTokenizeFormAng(args ...interface{}) (form aForm)

func NetaC added in v0.2.1

func NetaC(args ...interface{}) string

func NetaO added in v0.2.1

func NetaO(args ...interface{}) string

func Netadd added in v0.2.1

func Netadd(x, v float64) float64

func NetatForm added in v0.2.1

func NetatForm(args ...interface{}) string

func NetatInput added in v0.2.1

func NetatInput(args ...interface{}) string

func NetbatForm added in v0.2.1

func NetbatForm(d aForm) string

func NetbatInput added in v0.2.1

func NetbatInput(d afInput) string

func NetbtForm added in v0.2.1

func NetbtForm(d fForm) string

func NetbtInput added in v0.2.1

func NetbtInput(d fInput) string

func NetcastaForm added in v0.2.1

func NetcastaForm(args ...interface{}) *aForm

func NetcastafInput added in v0.2.1

func NetcastafInput(args ...interface{}) *afInput

func NetcastfForm added in v0.2.1

func NetcastfForm(args ...interface{}) *fForm

func NetcastfInput added in v0.2.1

func NetcastfInput(args ...interface{}) *fInput

func NetcatForm added in v0.2.1

func NetcatForm(args ...interface{}) (d aForm)

func NetcatInput added in v0.2.1

func NetcatInput(args ...interface{}) (d afInput)

func NetctForm added in v0.2.1

func NetctForm(args ...interface{}) (d fForm)

func NetctInput added in v0.2.1

func NetctInput(args ...interface{}) (d fInput)

func Netdivided added in v0.2.1

func Netdivided(x, v float64) float64

func Netimportcss added in v0.2.1

func Netimportcss(s string) string

func Netimportjs added in v0.2.1

func Netimportjs(s string) string

func Netmultiply added in v0.2.1

func Netmultiply(x, v float64) float64

func NetsessionDelete added in v0.2.1

func NetsessionDelete(s *sessions.Session) string

func NetsessionGet added in v0.2.1

func NetsessionGet(key string, s *sessions.Session) string

func NetsessionGetInt added in v0.2.1

func NetsessionGetInt(key string, s *sessions.Session) interface{}

func NetsessionKey added in v0.2.1

func NetsessionKey(key string, s *sessions.Session) bool

func NetsessionRemove added in v0.2.1

func NetsessionRemove(key string, s *sessions.Session) string

func NetsessionSet added in v0.2.1

func NetsessionSet(key string, value string, s *sessions.Session) string

func NetsessionSetInt added in v0.2.1

func NetsessionSetInt(key string, value interface{}, s *sessions.Session) string

func NetstructaForm added in v0.2.1

func NetstructaForm() *aForm

func NetstructafInput added in v0.2.1

func NetstructafInput() *afInput

func NetstructfForm added in v0.2.1

func NetstructfForm() *fForm

func NetstructfInput added in v0.2.1

func NetstructfInput() *fInput

func Netsubs added in v0.2.1

func Netsubs(x, v float64) float64

func NettForm added in v0.2.1

func NettForm(args ...interface{}) string

func NettInput added in v0.2.1

func NettInput(args ...interface{}) string

func Path

func Path(pathtoformat File) string

func ReadyTemplate added in v0.2.1

func ReadyTemplate(body []byte) string

func RestoreAsset added in v0.2.1

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets added in v0.2.1

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

func SetField added in v0.2.1

func SetField(obj interface{}, name string, value interface{}) error

func SetKey

func SetKey(key string)

func ToBson

func ToBson(data string) bson.M

func UrlAtZ added in v0.2.1

func UrlAtZ(url, base string) (isURL bool)

func ValidateRequest

func ValidateRequest(r *http.Request, usertoken string) bool

Returns bool : Verifies if request is valid

Types

type Date

type Date string

Display Date input. Return string of Date. Format YYYY-M-d

type Email

type Email string

Display Email input.

type File

type File string

Handle file upload. Use func Path to get a string path relative to your filesystem.

type NoStruct added in v0.2.1

type NoStruct struct {
}

type Page added in v0.2.1

type Page struct {
	Title string
	Body  []byte

	R       *http.Request
	Session *sessions.Session
	// contains filtered or unexported fields
}

type Paragraph

type Paragraph string

Display textarea

type Password added in v0.2.1

type Password []byte

Display Passworld input

type Radio

type Radio Select

Display radio input. Tag properties : title : title of field. select : comma delimited choices of field.

type SampleForm added in v0.2.1

type SampleForm struct {
	TestField string   `title:"Hi world!",valid:"unique",placeholder:"Testfield prompt"`
	Count     int      `placeholder:"Count"`
	Name      Password `valid:"required",title:"Input title"`
	FieldTwo  Radio    `title:"Enter Email",valid:"email,unique,required",select:"blue,orange,red,green"`
	FieldF    Select   `placeholder:"Prompt?",valid:"email,unique,required",select:"blue,orange,red,green"`
	Created   Date
	Text      Paragraph `title:"Enter a description."`
	Photo     File      `file:"image/*"`
	Emal      Email
	Terms     bool `title:"Accept terms of use."`
}

func NetcastSampleForm added in v0.2.1

func NetcastSampleForm(args ...interface{}) *SampleForm

func NetstructSampleForm added in v0.2.1

func NetstructSampleForm() *SampleForm

type Select

type Select string

Display dropdown. Tag properties : title : title of field. placeholder : Prompt left of field. select : comma delimited choices of field.

type SelectMult

type SelectMult Select

Display dropdown list with multiple selection support. Tag properties : title : title of field. placeholder : Prompt left of field. select : comma delimited choices of field.

Jump to

Keyboard shortcuts

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