form

package
v0.0.0-...-b75375f Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2014 License: MPL-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Before the input
	AddOnPositionBefore = iota
	// After the input
	AddOnPositionAfter
)
View Source
const (

	// NotChosen is used to indicate that a value from
	// a multiple choices input has not been chosen
	// by the user. See also Choose.
	NotChosen = notChosen
)

Variables

View Source
var (
	// Choose creates a choice entry with the text
	// "Please, choose" which generates an error when
	// the user does not choose anything.
	Choose = &Choice{
		Name:  i18n.String("form|Please, choose"),
		Value: NotChosen,
	}
)

Functions

func SetDefaultRenderer

func SetDefaultRenderer(f func() Renderer)

SetDefaultRenderer sets the function which will return a default renderer. The default value returns nil.

Types

type AddOn

type AddOn struct {
	// The HTML node to use as addon
	Node *html.Node
	// The addon position
	Position AddOnPosition
}

AddOn represents an addon which can be added to a form field. Keep in mind that not all renderers nor all types of fields support addons. Check each renderer's documentation for further information.

type AddOnPosition

type AddOnPosition int

AddOnPosition indicates the position of the addon relative to the field input.

type AddOnProvider

type AddOnProvider interface {
	// FieldAddOns returns the addons for the given field.
	// It's called just before the field is rendered.
	FieldAddOns(ctx *app.Context, field *Field) []*AddOn
}

AddOnProvider is an interface which might be implemented by types included in a form. If the type implements this interface, its function will be called for every field.

type Choice

type Choice struct {
	// Name must be either a string or an i18n.String
	// or a fmt.Stringer.
	// Other types will panic when rendering the form.
	Name  interface{}
	Value interface{}
}

Choice represents a choice in a select or radio field.

func (*Choice) TranslatedName

func (c *Choice) TranslatedName(lang i18n.Languager) string

type ChoicesProvider

type ChoicesProvider interface {
	// FieldChoices returns the choices for the given field.
	// It's called just before the field is rendered.
	FieldChoices(ctx *app.Context, field *Field) []*Choice
}

ChoicesProvider is the interface implemented by types which contain a field or type select or radio. The function will only be called for fields of those types. If a type which contains choices does not implement this interface, the form will panic on creation.

type Field

type Field struct {
	Type        Type
	Name        string
	HTMLName    string
	Label       i18n.String
	Placeholder i18n.String
	Help        i18n.String
	// contains filtered or unexported fields
}

func (*Field) Err

func (f *Field) Err() error

func (*Field) HasAddOns

func (f *Field) HasAddOns() bool

func (*Field) Id

func (f *Field) Id() string

func (*Field) SettableValue

func (f *Field) SettableValue() interface{}

func (*Field) String

func (f *Field) String() string

func (*Field) Tag

func (f *Field) Tag() *structs.Tag

func (*Field) Value

func (f *Field) Value() interface{}

type File

type File []interface{}

File represents an uploaded file. The slice fields should not be accessed directly, the helper methods should be used instead.

func (File) Close

func (f File) Close() error

func (File) Filename

func (f File) Filename() string

func (File) Header

func (f File) Header() *multipart.FileHeader

func (File) IsEmpty

func (f File) IsEmpty() bool

func (File) Read

func (f File) Read(p []byte) (n int, err error)

func (File) ReadAll

func (f File) ReadAll() ([]byte, error)

func (File) ReadAt

func (f File) ReadAt(p []byte, off int64) (n int, err error)

func (File) Seek

func (f File) Seek(offset int64, whence int) (int64, error)

type Form

type Form struct {

	// Don't include the field name in the error
	NamelessErrors bool
	// DisableCSRF disables CSRF protection when set
	// to true. Note that changing this after the
	// form has been rendered or validated has no effect.
	DisableCSRF bool
	// contains filtered or unexported fields
}

func New

func New(ctx *app.Context, values ...interface{}) *Form

New is a shorthand for NewOpts(ctx, nil, values...). See NewOpts for more information.

func NewOpts

func NewOpts(ctx *app.Context, opts *Options, values ...interface{}) *Form

NewOpts returns a new form using the given context, renderer and options.

If no Renderer is specified (either opts is nil or its Renderer field is nil), DefaultRenderer will be used to instantiate a renderer. Some packages, like gnd.la/bootstrap/form, override DefaultRenderer.

The values argument must contains pointers to structs.

Since any error generated during form creation will be a programming error, this function panics on errors.

Consult the package documentation for the the tags parsed by the form library.

func (*Form) FieldByName

func (f *Form) FieldByName(name string) (*Field, error)

func (*Form) FieldNames

func (f *Form) FieldNames() []string

func (*Form) Fields

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

func (*Form) HasErrors

func (f *Form) HasErrors() bool

func (*Form) Id

func (f *Form) Id() string

Id returns the prefix added to each field id in this form. Keep in mind that this function will never return an empty string because the form automatically generates a sufficiently unique id on creation.

func (*Form) IsValid

func (f *Form) IsValid() bool

func (*Form) Render

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

Render renders all the fields in the form, in the order specified during construction.

func (*Form) RenderExcept

func (f *Form) RenderExcept(names ...string) (template.HTML, error)

RenderExcept renders all the form's fields except the ones specified in the names parameter.

func (*Form) RenderOnly

func (f *Form) RenderOnly(names ...string) (template.HTML, error)

RenderOnly renders the given fields, identified by their names in the struct. If a field does not exist, an error is returned. Fields are rendered according to the order of the parameters passed to this function.

func (*Form) Renderer

func (f *Form) Renderer() Renderer

func (*Form) SetId

func (f *Form) SetId(id string)

SetId sets the prefix to be added to each field id attribute when rendering the form.

func (*Form) Submitted

func (f *Form) Submitted() bool

type Options

type Options struct {
	// Render is the type which renders the form. If Renderer
	// is nil, DefaultRenderer() is used to obtain a new Renderer.
	// When writing a reusable package or app, is recommended to always
	// leave this field with a nil value, so the app user can import
	// a package which redefines the default Renderer to use the
	// frontend framework used in the final app
	// (like e.g. gnd.la/bootstrap/form).
	Renderer Renderer
	// Fields lists the struct fields to include in the form. If empty,
	// all exported fields are included.
	Fields []string
}

Options specify the Form options at creation time.

type Renderer

type Renderer interface {
	// BeginField is called before starting to write any field.
	BeginField(w io.Writer, field *Field) error
	// BeginLabel is called before writing the label. It might be
	// called multiple times for radio fields. Renderers should use
	// the provided label string rather than the Label field on the
	// Field struct, because the former is already translated.
	BeginLabel(w io.Writer, field *Field, label string, pos int) error
	// LabelAttributes is called just before writing the <label> for the field.
	LabelAttributes(field *Field, pos int) (html.Attrs, error)
	// EndLabel is called before writing the end of the label. It might be
	// called multiple times for radio fields.
	EndLabel(w io.Writer, field *Field, pos int) error
	// BeginInput is called just before writing the <input> or equivalent tag
	// or any addons. For radio fields BeginInput will be called multiple times,
	// one per option. Renders should use the provided placeholder string
	// rather than the Placeholder field in the Field struct, because the
	// former is already translated.
	BeginInput(w io.Writer, field *Field, placeholder string, pos int) error
	// FieldAttributes is called just before writing the field (input, textarea, etc...)
	FieldAttributes(field *Field, pos int) (html.Attrs, error)
	// EndInput is called just after writing the <input> or equivalent tag and
	// all the addons. For radio fields EndInput will be called multiple times,
	// one per option.
	EndInput(w io.Writer, field *Field, pos int) error
	// WriteAddOn might be called multiple times, both before writing the field
	// and after (depending on the addons' positions). All these calls will happen
	// after BeginInput() and EndInput().
	WriteAddOn(w io.Writer, field *Field, addon *AddOn) error
	// WriteError is called only for fields which are in not valid, after
	// the label and the input have been written. err is translated before the
	// function is called.
	WriteError(w io.Writer, field *Field, err error) error
	// WriteHelp is called only for fields which have declared a help string, after
	// the label, the input and potentially the error have been written.
	// Renderers should use the provided help string rather than the Help field on the
	// Field struct, because the former is already translated.
	WriteHelp(w io.Writer, field *Field, help string) error
	// EndField is called after all the other field related functions have been called.
	EndField(w io.Writer, field *Field) error
}

Renderer is the interface implemented to help the form render its elements. Without a Renderer, the form won't render errors nor help messages. Except when noted otherwise, each function is called at most once for each non-hidden field included in the form.

func DefaultRenderer

func DefaultRenderer() Renderer

DefaultRenderer return a new Renderer using the default function.

type Type

type Type int
const (
	// <input type="text">
	TEXT Type = iota + 1
	// <input type="password">
	PASSWORD
	// <input type="hidden">
	HIDDEN
	// <textarea>
	TEXTAREA
	// <input type="checkbox">
	CHECKBOX
	// <input type="radio">
	RADIO
	// <select>
	SELECT
	// <input type="file">
	FILE
)

func (Type) HasChoices

func (t Type) HasChoices() bool

HasChoices returns wheter the type has multiple choices, which corresponds to RADIO and SELECT elements.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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