gforms

package module
v0.0.0-...-138c254 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2014 License: BSD-3-Clause Imports: 14 Imported by: 0

README

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>

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	WidgetTemplatePath   = rootDir() + "templates/gforms/widget.html"
	CheckboxTemplatePath = rootDir() + "templates/gforms/checkbox.html"
	RadioTemplatePath    = rootDir() + "templates/gforms/radio.html"
)
View Source
var (
	ErrRequired = errors.New("This field is required")
)

Functions

func InitForm

func InitForm(form Form) error

func IsFieldValid

func IsFieldValid(f Field, rawValue interface{}) bool

func IsFormSelfValid

func IsFormSelfValid(form Form) bool

func IsFormValid

func IsFormValid(form Form, formValues url.Values) bool

func IsMultipartFormValid

func IsMultipartFormValid(form Form, multipartForm *multipart.Form) bool

func IsValid

func IsValid(f Form, getValue valueGetterFunc) bool

func Register

func Register(field Field, constr constructor)

func Render

func Render(field Field, attrs ...string) (template.HTML, error)

func RenderError

func RenderError(f Field) (template.HTML, error)

func RenderErrors

func RenderErrors(form Form) (template.HTML, error)

func RenderField

func RenderField(f Field, attrs ...string) (template.HTML, error)

func RenderHiddenFields

func RenderHiddenFields(form Form) (template.HTML, error)

func RenderLabel

func RenderLabel(f Field) (template.HTML, error)

Types

type BaseField

type BaseField struct {
	// contains filtered or unexported fields
}

func (*BaseField) AddValidator

func (f *BaseField) AddValidator(validator Validator)

func (*BaseField) ApplyValidators

func (f *BaseField) ApplyValidators(rawValue interface{}) error

func (*BaseField) HasLabel

func (f *BaseField) HasLabel() bool

func (*BaseField) HasName

func (f *BaseField) HasName() bool

func (*BaseField) HasValidationError

func (f *BaseField) HasValidationError() bool

func (*BaseField) IsMulti

func (f *BaseField) IsMulti() bool

func (*BaseField) IsMultipart

func (f *BaseField) IsMultipart() bool

func (*BaseField) IsRequired

func (f *BaseField) IsRequired() bool

func (*BaseField) Label

func (f *BaseField) Label() string

func (*BaseField) Name

func (f *BaseField) Name() string

func (*BaseField) RawValue

func (f *BaseField) RawValue() interface{}

func (*BaseField) Render

func (f *BaseField) Render(attrs ...string) template.HTML

func (*BaseField) Reset

func (f *BaseField) Reset()

func (*BaseField) SetIsMulti

func (f *BaseField) SetIsMulti(flag bool)

func (*BaseField) SetIsMultipart

func (f *BaseField) SetIsMultipart(flag bool)

func (*BaseField) SetIsRequired

func (f *BaseField) SetIsRequired(flag bool)

func (*BaseField) SetLabel

func (f *BaseField) SetLabel(label string)

func (*BaseField) SetName

func (f *BaseField) SetName(name string)

func (*BaseField) SetValidationError

func (f *BaseField) SetValidationError(err error)

func (*BaseField) SetWidget

func (f *BaseField) SetWidget(widget Widget)

func (*BaseField) StringValue

func (f *BaseField) StringValue() string

func (*BaseField) ValidationError

func (f *BaseField) ValidationError() error

func (*BaseField) Widget

func (f *BaseField) Widget() Widget

type BaseForm

type BaseForm struct {
	// contains filtered or unexported fields
}

func (*BaseForm) Errors

func (f *BaseForm) Errors() map[string]error

func (*BaseForm) Fields

func (f *BaseForm) Fields() map[string]Field

func (*BaseForm) SetError

func (f *BaseForm) SetError(fieldname string, err error)

func (*BaseForm) SetErrors

func (f *BaseForm) SetErrors(errors map[string]error)

func (*BaseForm) SetFields

func (f *BaseForm) SetFields(fields map[string]Field)

func (*BaseForm) Validate

func (f *BaseForm) Validate() bool

type BaseWidget

type BaseWidget struct {
	HTML string
	// contains filtered or unexported fields
}

func (*BaseWidget) Attrs

func (w *BaseWidget) Attrs() *WidgetAttrs

func (*BaseWidget) IsHidden

func (w *BaseWidget) IsHidden() bool

func (*BaseWidget) Render

func (w *BaseWidget) Render(attrs []string, values ...string) template.HTML

type BoolField

type BoolField struct {
	*BaseField
}

func NewBoolField

func NewBoolField() *BoolField

func (*BoolField) Render

func (f *BoolField) Render(attrs ...string) template.HTML

func (*BoolField) SetInitial

func (f *BoolField) SetInitial(initial bool)

func (*BoolField) Validate

func (f *BoolField) Validate(rawValue interface{}) error

func (*BoolField) Value

func (f *BoolField) Value() bool

type CheckboxWidget

type CheckboxWidget struct {
	*BaseWidget
}

func NewCheckboxWidget

func NewCheckboxWidget() *CheckboxWidget

type ChoiceWidget

type ChoiceWidget interface {
	Widget
	SetChoices(choices [][2]string)
}

type Field

type Field interface {
	Validator

	HasName() bool
	SetName(string)
	Name() string

	HasLabel() bool
	SetLabel(string)
	Label() string

	SetWidget(Widget)
	Widget() Widget

	SetIsMulti(bool)
	IsMulti() bool
	SetIsMultipart(bool)
	IsMultipart() bool
	SetIsRequired(bool)
	IsRequired() bool

	AddValidator(Validator)
	ApplyValidators(interface{}) error

	HasValidationError() bool
	SetValidationError(error)
	ValidationError() error

	RawValue() interface{}

	Reset()
	Render(...string) template.HTML
}

type FileField

type FileField struct {
	*BaseField
}

func NewFileField

func NewFileField() *FileField

func (*FileField) Render

func (f *FileField) Render(attrs ...string) template.HTML

func (*FileField) SetInitial

func (f *FileField) SetInitial(initial *multipart.FileHeader)

func (*FileField) Validate

func (f *FileField) Validate(rawValue interface{}) error

func (*FileField) Value

func (f *FileField) Value() *multipart.FileHeader

type FileWidget

type FileWidget struct {
	*BaseWidget
}

func NewFileWidget

func NewFileWidget() *FileWidget

func (*FileWidget) Render

func (w *FileWidget) Render(attrs []string, values ...string) template.HTML

type Form

type Form interface {
	SetFields(map[string]Field)
	Fields() map[string]Field

	SetErrors(map[string]error)
	Errors() map[string]error

	Validate() bool
}

type HiddenWidget

type HiddenWidget struct {
	*BaseWidget
}

func NewHiddenWidget

func NewHiddenWidget() *HiddenWidget

func (*HiddenWidget) IsHidden

func (w *HiddenWidget) IsHidden() bool

func (*HiddenWidget) Render

func (w *HiddenWidget) Render(attrs []string, values ...string) template.HTML

type Int64Choice

type Int64Choice struct {
	Value int64
	Label string
}

type Int64ChoiceField

type Int64ChoiceField struct {
	*Int64Field
}

func NewRadioInt64Field

func NewRadioInt64Field() *Int64ChoiceField

func NewSelectInt64Field

func NewSelectInt64Field() *Int64ChoiceField

func (*Int64ChoiceField) SetChoices

func (f *Int64ChoiceField) SetChoices(choices []Int64Choice)

type Int64ChoicesValidator

type Int64ChoicesValidator struct {
	Choices []Int64Choice
}

func NewInt64ChoicesValidator

func NewInt64ChoicesValidator(choices []Int64Choice) *Int64ChoicesValidator

func (*Int64ChoicesValidator) Validate

func (v *Int64ChoicesValidator) Validate(rawValue interface{}) error

type Int64Field

type Int64Field struct {
	*BaseField
}

func NewInt64Field

func NewInt64Field() *Int64Field

func (*Int64Field) Render

func (f *Int64Field) Render(attrs ...string) template.HTML

func (*Int64Field) SetInitial

func (f *Int64Field) SetInitial(initial int64)

func (*Int64Field) Validate

func (f *Int64Field) Validate(rawValue interface{}) error

func (*Int64Field) Value

func (f *Int64Field) Value() int64

type MultiInt64ChoiceField

type MultiInt64ChoiceField struct {
	*Int64ChoiceField
}

func NewMultiSelectInt64Field

func NewMultiSelectInt64Field() *MultiInt64ChoiceField

func (*MultiInt64ChoiceField) Render

func (f *MultiInt64ChoiceField) Render(attrs ...string) template.HTML

func (*MultiInt64ChoiceField) SetInitial

func (f *MultiInt64ChoiceField) SetInitial(initial []int64)

func (*MultiInt64ChoiceField) StringValue

func (f *MultiInt64ChoiceField) StringValue() []string

func (*MultiInt64ChoiceField) Validate

func (f *MultiInt64ChoiceField) Validate(rawValue interface{}) error

func (*MultiInt64ChoiceField) Value

func (f *MultiInt64ChoiceField) Value() []int64

type MultiStringChoiceField

type MultiStringChoiceField struct {
	*StringChoiceField
}

func NewMultiSelectStringField

func NewMultiSelectStringField() *MultiStringChoiceField

func (*MultiStringChoiceField) Render

func (f *MultiStringChoiceField) Render(attrs ...string) template.HTML

func (*MultiStringChoiceField) SetInitial

func (f *MultiStringChoiceField) SetInitial(initial []string)

func (*MultiStringChoiceField) StringValue

func (f *MultiStringChoiceField) StringValue() []string

func (*MultiStringChoiceField) Validate

func (f *MultiStringChoiceField) Validate(rawValue interface{}) error

func (*MultiStringChoiceField) Value

func (f *MultiStringChoiceField) Value() []string

type MultiValueField

type MultiValueField interface {
	StringValue() []string
}

type RadioWidget

type RadioWidget struct {
	*BaseWidget
	// contains filtered or unexported fields
}

func NewRadioWidget

func NewRadioWidget() *RadioWidget

func (*RadioWidget) Radios

func (w *RadioWidget) Radios(attrs []string, checkedValue string) []template.HTML

func (*RadioWidget) Render

func (w *RadioWidget) Render(attrs []string, checkedValues ...string) template.HTML

func (*RadioWidget) SetChoices

func (w *RadioWidget) SetChoices(choices [][2]string)

type SelectWidget

type SelectWidget struct {
	*BaseWidget
	// contains filtered or unexported fields
}

func NewMultiSelectWidget

func NewMultiSelectWidget() *SelectWidget

func NewSelectWidget

func NewSelectWidget() *SelectWidget

func (*SelectWidget) Options

func (w *SelectWidget) Options(selValues ...string) []string

func (*SelectWidget) Render

func (w *SelectWidget) Render(attrs []string, values ...string) template.HTML

func (*SelectWidget) SetChoices

func (w *SelectWidget) SetChoices(choices [][2]string)

type SingleValueField

type SingleValueField interface {
	StringValue() string
}

type StringChoice

type StringChoice struct {
	Value string
	Label string
}

type StringChoiceField

type StringChoiceField struct {
	*StringField
}

func NewRadioStringField

func NewRadioStringField() *StringChoiceField

func NewSelectStringField

func NewSelectStringField() *StringChoiceField

func (*StringChoiceField) SetChoices

func (f *StringChoiceField) SetChoices(choices []StringChoice)

type StringChoicesValidator

type StringChoicesValidator struct {
	Choices []StringChoice
}

func NewStringChoicesValidator

func NewStringChoicesValidator(choices []StringChoice) *StringChoicesValidator

func (*StringChoicesValidator) Validate

func (v *StringChoicesValidator) Validate(rawValue interface{}) error

type StringField

type StringField struct {
	*BaseField
	MinLen, MaxLen int
}

func NewStringField

func NewStringField() *StringField

func (*StringField) Render

func (f *StringField) Render(attrs ...string) template.HTML

func (*StringField) SetInitial

func (f *StringField) SetInitial(initial string)

func (*StringField) Validate

func (f *StringField) Validate(rawValue interface{}) error

func (*StringField) Value

func (f *StringField) Value() string

type TextWidget

type TextWidget struct {
	*BaseWidget
}

func NewTextWidget

func NewTextWidget() *TextWidget

type TextareaStringField

type TextareaStringField struct {
	*StringField
}

func NewTextareaStringField

func NewTextareaStringField() *TextareaStringField

type TextareaWidget

type TextareaWidget struct {
	*BaseWidget
}

func NewTextareaWidget

func NewTextareaWidget() *TextareaWidget

type Validator

type Validator interface {
	Validate(interface{}) error
}

type Widget

type Widget interface {
	IsHidden() bool
	Attrs() *WidgetAttrs
	Render([]string, ...string) template.HTML
}

type WidgetAttrs

type WidgetAttrs struct {
	// contains filtered or unexported fields
}

func (*WidgetAttrs) Clone

func (w *WidgetAttrs) Clone() *WidgetAttrs

func (*WidgetAttrs) FromSlice

func (w *WidgetAttrs) FromSlice(attrs []string)

func (*WidgetAttrs) Get

func (w *WidgetAttrs) Get(name string) (string, bool)

func (*WidgetAttrs) Names

func (w *WidgetAttrs) Names() []string

func (*WidgetAttrs) Pop

func (w *WidgetAttrs) Pop(name string) (string, bool)

func (*WidgetAttrs) Set

func (w *WidgetAttrs) Set(name, value string)

func (*WidgetAttrs) String

func (w *WidgetAttrs) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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