formbuilder

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 16 Imported by: 0

README

Form Builder plugin

plugins/formbuilder adds forms and form-submissions collections and validates every submission against its selected form. Pair it with @riducms/plugin-form-builder for the admin UI and frontend helpers.

The default fields are checkbox, country, email, message, number, select, state, text, and textarea. Date, radio, upload, and payment fields are opt-in.

formbuilder.New(formbuilder.Config{
	EnabledFields: []formbuilder.FieldType{
		formbuilder.FieldText,
		formbuilder.FieldEmail,
		formbuilder.FieldTextarea,
		formbuilder.FieldUpload,
	},
	UploadCollections:     []schema.CollectionSlug{"media"},
	RedirectRelationships: []schema.CollectionSlug{"pages"},
	DefaultToEmail:        "forms@example.com",
	SendEmail: func(ctx context.Context, email formbuilder.Email) error {
		return mailer.Send(ctx, email)
	},
})

Form definitions are public by default so application frontends can render them. Their emails field is visible only to the configured admin-user collection. Form mutations and submission reads and deletes require that admin identity; submissions allow anonymous create and reject updates. Collection overrides may replace those defaults, while the plugin keeps the collection identity and the validation hooks required by the pair.

Email delivery runs after commit. {{field}}, {{*}}, {{*:table}}, and {{formSubmissionID}} placeholders are supported; submission-derived HTML is escaped. The application supplies the mail provider and credentials through SendEmail. Delivery failures are reported through ReportError or structured logging and do not roll back an already committed submission.

Payment work runs before commit. Configure PaymentProcessors and HandlePayment; exactly one payment block may appear in a form. An omitted optional payment does no provider work. The callback receives the server-calculated finite total and returns the JSON value stored with the submission. An error aborts the write, but provider side effects must still be idempotent because they are not part of the database transaction. Upload submissions use string IDs for a single configured target or Ridu's {relationTo, id} shape for polymorphic targets. They are checked against the configured collection, existence and read access, MIME allowlist, file-size limit, and singular/multiple setting; missing protected metadata fails closed.

Fields can also append custom block types. The plugin preserves their stored submission values and enforces identity and required presence; provide their renderer and any additional validation.

See the Form Builder guide for setup, field overrides, validation, email, payments, migrations, and verification.

Documentation

Overview

Package formbuilder contributes Payload-familiar dynamic form definitions, submission persistence, validation, payment callbacks, and email delivery.

Index

Constants

View Source
const (
	// Key is the stable backend/admin plugin identity.
	Key = "form-builder"
	// AdminPluginPairingVersion changes when the Go and TypeScript packages no
	// longer understand the same generated form contract.
	AdminPluginPairingVersion = 1
	// DefaultFormsSlug is the collection containing reusable form definitions.
	DefaultFormsSlug schema.CollectionSlug = "forms"
	// DefaultSubmissionsSlug is the collection containing captured responses.
	DefaultSubmissionsSlug schema.CollectionSlug = "form-submissions"
)

Variables

This section is empty.

Functions

func GetPaymentTotal

func GetPaymentTotal(basePrice float64, conditions []PriceCondition, values map[string]store.Value) (float64, error)

GetPaymentTotal applies conditions in order, matching Payload's form-builder price model while rejecting non-finite arithmetic and division by zero.

Types

type BeforeEmail

type BeforeEmail func(EmailContext, []Email) ([]Email, error)

BeforeEmail can wrap templates, filter recipients, or otherwise transform the complete detached email batch before delivery.

type CollectionOverride

type CollectionOverride func(defaultCollection ridu.Collection) (ridu.Collection, error)

CollectionOverride receives a detached generated collection and returns its complete replacement. The plugin reasserts lifecycle invariants afterward.

type Config

type Config struct {
	FormsSlug             schema.CollectionSlug
	SubmissionsSlug       schema.CollectionSlug
	EnabledFields         []FieldType
	UploadCollections     []schema.CollectionSlug
	RedirectRelationships []schema.CollectionSlug
	PaymentProcessors     []field.Choice
	Fields                FieldsOverride
	Forms                 CollectionOverride
	Submissions           CollectionOverride
	DefaultToEmail        string
	BeforeEmail           BeforeEmail
	SendEmail             SendEmail
	HandlePayment         HandlePayment
	ReportError           func(error, string)
}

Config customizes the generated collections and executable form lifecycle.

type Email

type Email struct {
	To      string
	CC      string
	BCC     string
	From    string
	ReplyTo string
	Subject string
	HTML    string
}

Email is one fully formatted outbound message. Delivery remains application owned so provider credentials never enter the plugin manifest or documents.

type EmailContext

type EmailContext struct {
	Context    ridu.HookContext
	Form       store.Document
	Submission store.Document
}

EmailContext identifies the committed submission that produced messages.

type FieldType

type FieldType string

FieldType identifies one dynamic input block available to editors.

const (
	FieldCheckbox FieldType = "checkbox"
	FieldCountry  FieldType = "country"
	FieldDate     FieldType = "date"
	FieldEmail    FieldType = "email"
	FieldMessage  FieldType = "message"
	FieldNumber   FieldType = "number"
	FieldPayment  FieldType = "payment"
	FieldRadio    FieldType = "radio"
	FieldSelect   FieldType = "select"
	FieldState    FieldType = "state"
	FieldText     FieldType = "text"
	FieldTextarea FieldType = "textarea"
	FieldUpload   FieldType = "upload"
)

type FieldsOverride

type FieldsOverride func(defaultFields []field.Block) ([]field.Block, error)

FieldsOverride receives detached default block definitions and returns the complete ordered set available in the form builder.

type HandlePayment

type HandlePayment func(PaymentContext) (store.Value, error)

HandlePayment performs application-owned payment work and returns the JSON value persisted with the submission. Returning an error aborts the write.

type PaymentContext

type PaymentContext struct {
	Context        ridu.HookContext
	Form           store.Document
	Field          store.Values
	SubmissionData map[string]store.Value
	Total          float64
}

PaymentContext is the trusted input supplied before a submission commits.

type Plugin

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

Plugin is one configured Form Builder extension.

func New

func New(config Config) *Plugin

New returns one compiled plugin. Slice-bearing config is detached so later application mutation cannot change the resolved schema or runtime behavior.

func (*Plugin) Descriptor

func (*Plugin) Descriptor() ridu.PluginDescriptor

Descriptor exposes deterministic backend/admin pairing metadata.

func (*Plugin) Key

func (*Plugin) Key() string

Key implements ridu.Plugin.

func (*Plugin) TransformConfig

func (plugin *Plugin) TransformConfig(config ridu.Config) (ridu.Config, error)

TransformConfig adds the Forms and Form Submissions collections through the same public config surface available to application code.

type PriceCondition

type PriceCondition struct {
	FieldToUse        string
	Condition         string
	ValueForCondition string
	Operator          string
	ValueType         string
	ValueForOperator  string
}

PriceCondition describes one conditional arithmetic adjustment.

type SendEmail

type SendEmail func(stdcontext.Context, Email) error

SendEmail delivers one formatted email using application-owned credentials.

Jump to

Keyboard shortcuts

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