Documentation ¶
Index ¶
- Constants
- Variables
- func SetDefaultRenderer(f func() Renderer)
- type AddOn
- type AddOnPosition
- type AddOnProvider
- type Choice
- type ChoicesProvider
- type Field
- type File
- func (f File) Close() error
- func (f File) Filename() string
- func (f File) Header() *multipart.FileHeader
- func (f File) IsEmpty() bool
- func (f File) Read(p []byte) (n int, err error)
- func (f File) ReadAll() ([]byte, error)
- func (f File) ReadAt(p []byte, off int64) (n int, err error)
- func (f File) Seek(offset int64, whence int) (int64, error)
- type Form
- func (f *Form) FieldByName(name string) (*Field, error)
- func (f *Form) FieldNames() []string
- func (f *Form) Fields() []*Field
- func (f *Form) HasErrors() bool
- func (f *Form) Id() string
- func (f *Form) IsValid() bool
- func (f *Form) Render() (template.HTML, error)
- func (f *Form) RenderExcept(names ...string) (template.HTML, error)
- func (f *Form) RenderOnly(names ...string) (template.HTML, error)
- func (f *Form) Renderer() Renderer
- func (f *Form) SetId(id string)
- func (f *Form) Submitted() bool
- type Options
- type Renderer
- type Type
Constants ¶
const ( // Before the input AddOnPositionBefore = iota // After the input AddOnPositionAfter )
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 ¶
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.
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) SettableValue ¶
func (f *Field) SettableValue() 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) Header ¶
func (f File) Header() *multipart.FileHeader
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 NewOpts ¶
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) FieldNames ¶
func (*Form) Id ¶
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) Render ¶
Render renders all the fields in the form, in the order specified during construction.
func (*Form) RenderExcept ¶
RenderExcept renders all the form's fields except the ones specified in the names parameter.
func (*Form) RenderOnly ¶
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.
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
func (Type) HasChoices ¶
HasChoices returns wheter the type has multiple choices, which corresponds to RADIO and SELECT elements.