npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

payload-plugin-form-builder

v1.0.8

Published

Form builder plugin for Payload CMS

Downloads

260

Readme

Notice: This plugin has moved! It is now supported by PayloadCMS — see the official plugin for full details.


Payload Form Builder Plugin

NPM

A plugin for Payload CMS to easily allow your admin editors to build and manage forms from the admin panel.

Core features:

  • Creates a forms collection where you can:
    • Build dynamic forms with any number of fields
    • Add payment fields that can handle dynamic prices
    • Build completely custom and dynamic emails
  • Creates a formSubmissions collection that:
    • Validates and saves the form data submitted by your frontend
    • Sends emails (if applicable)
    • Handles payment processing (if applicable)

Installation

  yarn add @payloadcms/plugin-form-builder
  # OR
  npm i @payloadcms/plugin-form-builder

Basic Usage

In the plugins array of your Payload config, call the plugin with options:

import { buildConfig } from 'payload/config';
import formBuilder from '@payloadcms/plugin-form-builder';

const config = buildConfig({
  collections: [
    {
      slug: 'pages',
      fields: []
    }
  ],
  plugins: [
    formBuilder()
  ]
});

export default config;

Options

  • fields An object of field types to allow your admin editors to build forms with. Pass either a boolean value or a partial Payload Block to override default settings. See Fields for more details.

    fields: {
      text: true,
      select: true,
      email: true,
      state: true,
      country: true,
      checkbox: true,
      number: true,
      message: true,
      payment: false
    }

    You can also provide your own custom field definitions by passing a new Payload Block object into fields.

  • redirectRelationships

    An array of collection slugs that, when enabled, are populated as options in form redirect fields.

    redirectRelationships: ['pages']
  • handlePayment

    A beforeChange hook that is called upon form submissions. You can integrate into any third-party payment processing API here. There is a getPaymentTotal function that will calculate the total cost after all conditions have been applied.

    import { getPaymentTotal } from '@payloadcms/plugin-form-builder';
    ...
    handlePayment: async ({ form, submissionData }) => {
      // first calculate the price
      const paymentField =  form.fields?.find((field) => field.blockType === 'payment');
      const price = getPaymentTotal({
        basePrice: paymentField.basePrice,
        priceConditions: paymentField.priceConditions,
        fieldValues: submissionData,
      });
      // then asynchronously process the payment here
    }
  • beforeEmail

    A beforeChange hook that is called just after emails are prepared, but before they are sent. This is a great place to inject your own HTML template to add custom styles.

    beforeEmails: (emailsToSend) => {
      // modify the emails in any way before they are sent
      return emails.map((email) => ({
        ...email,
        html: email.html // transform the html in any way you'd like (maybe wrap it in an html template?)
      }))
    };
  • formOverrides

    Override anything on the form collection by sending a Payload Collection Config. Your overrides will be merged into the default forms collection.

    formOverrides: {
      slug: 'contact-forms'
    }
  • formSubmissionOverrides By default, this plugin relies on Payload access control to restrict the update and read operations. This is because anyone should be able to create a form submission, even from a public-facing website - but no one should be able to update a submission one it has been created, or read a submission unless they have permission.

    You can override access control and anything else on the form submission collection by sending a Payload Collection Config. Your overrides will be merged into the default formSubmissions collection.

    formSubmissionOverrides: {
      slug: 'leads'
    }

Fields

Each form field is defined as a Payload Block with the following subfields:

  • Text
    • name: string
    • label: string
    • defaultValue: string
    • width: string
    • required: checkbox
  • Select
    • name: string
    • label: string
    • defaultValue: string
    • width: string
    • options: array
    • required: checkbox
  • Email
    • name: string
    • label: string
    • defaultValue: string
    • width: string
    • required: checkbox
  • State
    • name: string
    • label: string
    • defaultValue: string
    • width: string
    • required: checkbox
  • Country
    • name: string
    • label: string
    • defaultValue: string
    • width: string
    • required: checkbox
  • Checkbox
    • name: string
    • label: string
    • defaultValue: checkbox
    • width: string
    • required: checkbox
  • Number
    • name: string
    • label: string
    • defaultValue: number
    • width: string
    • required: checkbox
  • Message
    • message: richText
  • Payment
    • name: string
    • label: string
    • defaultValue: number
    • width: string
    • required: checkbox
    • priceConditions: array
      • fieldToUse: relationship, dynamically populated based on the fields in your form
      • condition: string - equals, notEquals | hasValue
      • valueForOperator: string - only if condition is equals or notEquals
      • operator: string - add, subtract, multiply, divide
      • valueType: string - static, valueOfField
      • value: string - only if valueType is static

Email

This plugin relies on the email configuration defined in your payload.init(). It will read from your config and attempt to send your emails using the credentials provided.

TypeScript

All types can be directly imported:

import {
  FormConfig,
  Form,
  FormSubmission,
  FieldsConfig,
  BeforePayment,
  HandlePayment
 } from '@payloadcms/plugin-form-builder/dist/types';

Screenshots

screenshot 1 screenshot 2 screenshot 3 screenshot 4 screenshot 5 screenshot 6