Skip to content

Commit

Permalink
feat(plugin-form-builder)!: update form builder plugin field override…
Browse files Browse the repository at this point in the history
…s to use a function instead (#6497)

## Description

Changes the `fields` override for form builder plugin to use a function
instead so that we can actually override existing fields which currently
will not work.

```ts
//before
fields: [
  {
    name: 'custom',
    type: 'text',
  }
]

// current
fields: ({ defaultFields }) => {
  return [
    ...defaultFields,
    {
      name: 'custom',
      type: 'text',
    },
  ]
}
```

## Type of change

- [x] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
  • Loading branch information
paulpopus authored May 28, 2024
1 parent b2662ee commit 7d0e909
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 242 deletions.
143 changes: 74 additions & 69 deletions packages/plugin-form-builder/src/collections/FormSubmissions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CollectionConfig } from 'payload/types'
import type { CollectionConfig, Field } from 'payload/types'

import type { FormBuilderPluginConfig } from '../../types.js'

Expand All @@ -11,6 +11,74 @@ export const generateSubmissionCollection = (
): CollectionConfig => {
const formSlug = formConfig?.formOverrides?.slug || 'forms'

const defaultFields: Field[] = [
{
name: 'form',
type: 'relationship',
admin: {
readOnly: true,
},
relationTo: formSlug,
required: true,
validate: async (value, { req: { payload }, req }) => {
/* Don't run in the client side */
if (!payload) return true

if (payload) {
let _existingForm

try {
_existingForm = await payload.findByID({
id: value,
collection: formSlug,
req,
})

return true
} catch (error) {
return 'Cannot create this submission because this form does not exist.'
}
}
},
},
{
name: 'submissionData',
type: 'array',
admin: {
readOnly: true,
},
fields: [
{
name: 'field',
type: 'text',
required: true,
},
{
name: 'value',
type: 'text',
required: true,
validate: (value: unknown) => {
// TODO:
// create a validation function that dynamically
// relies on the field type and its options as configured.

// How to access sibling data from this field?
// Need the `name` of the field in order to validate it.

// Might not be possible to use this validation function.
// Instead, might need to do all validation in a `beforeValidate` collection hook.

if (typeof value !== 'undefined') {
return true
}

return 'This field is required.'
},
},
],
},
]

const newConfig: CollectionConfig = {
...(formConfig?.formSubmissionOverrides || {}),
slug: formConfig?.formSubmissionOverrides?.slug || 'form-submissions',
Expand All @@ -24,74 +92,11 @@ export const generateSubmissionCollection = (
...(formConfig?.formSubmissionOverrides?.admin || {}),
enableRichTextRelationship: false,
},
fields: [
{
name: 'form',
type: 'relationship',
admin: {
readOnly: true,
},
relationTo: formSlug,
required: true,
validate: async (value, { req: { payload }, req }) => {
/* Don't run in the client side */
if (!payload) return true

if (payload) {
let _existingForm

try {
_existingForm = await payload.findByID({
id: value,
collection: formSlug,
req,
})

return true
} catch (error) {
return 'Cannot create this submission because this form does not exist.'
}
}
},
},
{
name: 'submissionData',
type: 'array',
admin: {
readOnly: true,
},
fields: [
{
name: 'field',
type: 'text',
required: true,
},
{
name: 'value',
type: 'text',
required: true,
validate: (value: unknown) => {
// TODO:
// create a validation function that dynamically
// relies on the field type and its options as configured.

// How to access sibling data from this field?
// Need the `name` of the field in order to validate it.

// Might not be possible to use this validation function.
// Instead, might need to do all validation in a `beforeValidate` collection hook.

if (typeof value !== 'undefined') {
return true
}

return 'This field is required.'
},
},
],
},
...(formConfig?.formSubmissionOverrides?.fields || []),
],
fields:
formConfig?.formSubmissionOverrides?.fields &&
typeof formConfig?.formSubmissionOverrides?.fields === 'function'
? formConfig?.formSubmissionOverrides?.fields({ defaultFields })
: defaultFields,
hooks: {
...(formConfig?.formSubmissionOverrides?.hooks || {}),
beforeChange: [
Expand Down
Loading

0 comments on commit 7d0e909

Please sign in to comment.