Documentation
¶
Overview ¶
Package forms adds a public form, such as a contact form or a quote request, with an inbox for its submissions. Each instance is one form; add one instance per form and place it on a page:
app.Use(forms.Module("contact", "Contact"), forms.Module("quote", "Quote request"))
Editors define the fields in the admin, one per line (see the Fields help text in the editor), with labels per language.
The website renders forms/form.gohtml with a FormView as its data. A minimal template:
{{/* @dot github.com/donseba/go-webthings/modules/forms.FormView */}}
<h1>{{.Title}}</h1>
{{if .Sent}}{{.Success}}{{else}}
{{.Intro}}
{{form_render .Form .Errors}}
{{end}}
form_render renders every field with go-form (see FormView.Form): its label, help text, placeholder and limits, the visitor's answer and the errors of a post at their fields, in the page's language, with the spam trap and a Send button. A template that lays out the fields itself uses {{form_open .Form}}, {{form_field .Form name .Errors}} per name of Fields and {{form_close}}.
The form posts to its own page, so a post with errors shows the form again with the visitor's answers, Errors and Invalid set. Like the visitor actions of other modules (see webthings.Env.Actions) it rejects cross-site posts (http.CrossOriginProtection), tells bots that fill in the trap that the form was sent without storing anything, and accepts 10 posts per visitor in 10 minutes: after that the form is shown again with Throttled set and nothing stored. After a valid submission the visitor returns to the page with ?sent=1 and Sent is set.
When the form has a notification address, every submission is mailed there with the answers and a link to it in the admin; replying goes to the visitor's address when the form has an email field. The mail is the mail template notify (see webthings.Module.Mail) with NotifyView as data, in the admin's language; a theme replaces it with mail/<key>/notify.gohtml or mail/forms/notify.gohtml. Submissions arrive as new and can be archived in the admin once handled.
Index ¶
- Constants
- func Module(key, title string) webthings.Module
- func ParseFields(source string) (FieldList, Labels, error)
- type Answer
- type Answers
- type Definition
- type Field
- type FieldList
- type FormView
- type InboxRow
- type InboxView
- type Item
- type Label
- type Labels
- type NotifyView
- type Service
- func (s *Service) Archive(ctx context.Context, id string) error
- func (s *Service) Count(ctx context.Context, status string) (int, error)
- func (s *Service) DeleteSubmission(ctx context.Context, id string) error
- func (s *Service) PurgeSubmission(ctx context.Context, id string) error
- func (s *Service) Store() content.Store[Definition, Text]
- func (s *Service) Submission(ctx context.Context, id string) (Submission, error)
- func (s *Service) SubmissionsByStatus(ctx context.Context, status string, offset, limit int) ([]Submission, bool, error)
- func (s *Service) TrashedSubmissions(ctx context.Context, limit, offset int) ([]Submission, int, error)
- func (s *Service) Unarchive(ctx context.Context, id string) error
- func (s *Service) UndeleteSubmission(ctx context.Context, id string) error
- type Submission
- type SubmissionView
- type Text
Constants ¶
const ( // StatusNew is a submission no editor has handled yet; the admin // counts them in a badge. StatusNew = "new" // StatusArchived is a submission an editor has handled. StatusArchived = "archived" )
Statuses of a submission: new until an editor archives it.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Answer ¶
type Answer struct {
Name string `json:"name"`
Label string `json:"label"`
Value string `json:"value"`
}
Answer is one submitted value, with the label the visitor saw.
type Definition ¶
Definition is the language-independent part of a form. Notify is the address new submissions are mailed to; empty sends no mail.
type Field ¶
type Field struct {
Name string `json:"name"`
Type string `json:"type"`
Required bool `json:"required,omitempty"`
Options []string `json:"options,omitempty"`
}
Field is one input of the form. Labels are translated; see Label.
type FormView ¶
type FormView struct {
Title string
Intro template.HTML
// Action is the URL the form posts to: the page itself.
Action string
// Form is the form's model: its fields as the editor defined them, in
// the page's language, with the visitor's answers after a post with
// errors, for go-form's template functions, such as {{form_render
// .Form .Errors}}, or {{form_open .Form}} with {{form_field .Form name
// .Errors}} per name of Fields. form_open adds the spam trap
// (frontend.Honeypot), and the fields' ids start with the instance key.
Form form.RenderModel
// Fields are the names of the form's fields, in order, for form_field.
Fields []string
// Errors are the problems of a post per field, with messages in the
// page's language, which form_field and form_render show at their
// fields; empty otherwise.
Errors form.FieldErrors
// Invalid is set when a submission has errors; see Errors.
Invalid bool
// Throttled is set when the visitor sent the form too often in a short
// time. Nothing was stored; the form is shown with their answers, to send
// again later.
Throttled bool
// Sent is set after a successful submission; show Success instead of the form.
Sent bool
Success template.HTML
}
FormView is the data of forms/form.gohtml.
type InboxView ¶
type InboxView struct {
Title string
BackURL string
// TrashURL shows the deleted submissions in the admin's trash.
TrashURL string
// Filters switch between new, archived and all submissions.
Filters admin.Tabs
Rows []InboxRow
Pager admin.Pager
}
InboxView is the data of templates/admin/inbox.gohtml: the submissions of a form, filtered by status.
type Label ¶
type Label struct {
Label string `json:"label"`
Placeholder string `json:"placeholder,omitempty"`
Help string `json:"help,omitempty"`
Options map[string]string `json:"options,omitempty"`
}
Label is the text of one field in one language. Options maps option values to their labels.
type NotifyView ¶
type NotifyView struct {
// Title is the form's title in the language the visitor used.
Title string
// Locale is the language the visitor used.
Locale string
// Answers are the submitted values, each with the label the visitor saw.
Answers Answers
// ReplyTo is the first address the visitor gave in an email field; empty
// when there is none.
ReplyTo string
// SubmissionURL opens the submission in the admin.
SubmissionURL string
}
NotifyView is the data of the mail template notify, sent to the form's notification address in the admin's language. Replying answers the visitor when the form has an email field.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is one form, for the website's own code.
func (*Service) Count ¶
Count returns the number of submissions with status; an empty status counts all.
func (*Service) DeleteSubmission ¶
DeleteSubmission moves a submission to the trash: it leaves the inbox and the counts, and UndeleteSubmission brings it back. It returns db.ErrNotFound when the submission is not in the inbox.
func (*Service) PurgeSubmission ¶
PurgeSubmission removes a submission in the trash for good. It returns db.ErrNotFound when it is not in the trash.
func (*Service) Store ¶
func (s *Service) Store() content.Store[Definition, Text]
Store gives write access to this instance's form definition, for imports and seeds. Admin edits go through the same store.
func (*Service) Submission ¶
Submission returns one submission that is not in the trash, or db.ErrNotFound.
func (*Service) SubmissionsByStatus ¶
func (s *Service) SubmissionsByStatus(ctx context.Context, status string, offset, limit int) ([]Submission, bool, error)
SubmissionsByStatus returns the submissions with one status, StatusNew or StatusArchived, newest first, skipping offset, and whether older ones exist; an empty status lists all. A limit of 0 or less returns every submission after offset.
func (*Service) TrashedSubmissions ¶
func (s *Service) TrashedSubmissions(ctx context.Context, limit, offset int) ([]Submission, int, error)
TrashedSubmissions returns submissions in the trash, most recently deleted first: at most limit of them from offset (all for a limit of 0), and how many the trash holds in all.
type Submission ¶
type Submission struct {
ID string `db:"id"`
Locale string `db:"locale"`
Status string `db:"status"`
Answers Answers `db:"answers"`
CreatedAt time.Time `db:"created_at"`
DeletedAt *time.Time `db:"deleted_at"`
DeletedBy db.Ref `db:"deleted_by"`
}
Submission is one sent form. DeletedAt and DeletedBy are set for submissions in the trash (see TrashedSubmissions).
type SubmissionView ¶
type SubmissionView struct {
Title string
BackURL string
Date string
Locale string
Archived bool
Answers Answers
// Archive or Unarchive is set, depending on the status, for editors.
Archive *form.RenderModel
Unarchive *form.RenderModel
Delete *form.RenderModel
}
SubmissionView is the data of templates/admin/submission.gohtml: one submission with every answer.