Skip to content

Commit 104a5fc

Browse files
authored
feat(plugin-form-builder): allow creating form submissions from the admin panel (#11222)
Fixes #10952. The form builder plugin does not currently allow creating form submissions from within the admin panel. This is because the fields of the form submissions collection have `admin.readOnly` set, ultimately disabling them during the create operation. Instead of doing this, the user's permissions should dictate whether these fields are read-only using access control. For example, based on role: ```ts import { buildConfig } from 'payload' import { formBuilderPlugin } from '@payloadcms/plugin-form-builder' export default buildConfig({ // ... plugins: [ formBuilderPlugin({ formSubmissionOverrides: { access: { update: ({ req }) => Boolean(req.user?.roles?.includes('admin')), }, }, }), ], }) ``` --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1211454879207842
1 parent ae7b75a commit 104a5fc

File tree

6 files changed

+57
-33
lines changed

6 files changed

+57
-33
lines changed

packages/plugin-form-builder/src/collections/FormSubmissions/fields/defaultPaymentFields.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import type { Field } from 'payload'
33
export const defaultPaymentFields: Field = {
44
name: 'payment',
55
type: 'group',
6-
admin: {
7-
readOnly: true,
8-
},
96
fields: [
107
{
118
name: 'field',

packages/plugin-form-builder/src/collections/FormSubmissions/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ export const generateSubmissionCollection = (
1818
{
1919
name: 'form',
2020
type: 'relationship',
21-
admin: {
22-
readOnly: true,
23-
},
2421
relationTo: formSlug,
2522
required: true,
2623
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
@@ -50,9 +47,6 @@ export const generateSubmissionCollection = (
5047
{
5148
name: 'submissionData',
5249
type: 'array',
53-
admin: {
54-
readOnly: true,
55-
},
5650
fields: [
5751
{
5852
name: 'field',

test/plugin-form-builder/collections/Users.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ export const Users: CollectionConfig = {
1010
read: () => true,
1111
},
1212
fields: [
13-
// Email added by default
14-
// Add more fields as needed
13+
{
14+
name: 'roles',
15+
type: 'select',
16+
hasMany: true,
17+
options: [
18+
{
19+
label: 'Admin',
20+
value: 'admin',
21+
},
22+
],
23+
},
1524
],
1625
}

test/plugin-form-builder/config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ export default buildConfigWithDefaults({
5454
data: {
5555
email: devUser.email,
5656
password: devUser.password,
57+
roles: ['admin'],
5758
},
5859
})
5960

6061
await seed(payload)
6162
},
62-
//email: nodemailerAdapter(),
63+
// email: nodemailerAdapter(),
6364
plugins: [
6465
formBuilderPlugin({
6566
// handlePayment: handleFormPayments,
66-
//defaultToEmail: 'devs@payloadcms.com',
67+
// defaultToEmail: 'devs@payloadcms.com',
6768
fields: {
6869
colorField,
6970
payment: true,
@@ -123,6 +124,9 @@ export default buildConfigWithDefaults({
123124
},
124125
},
125126
formSubmissionOverrides: {
127+
access: {
128+
update: ({ req }) => Boolean(req.user?.roles?.includes('admin')),
129+
},
126130
fields: ({ defaultFields }) => {
127131
const modifiedFields: Field[] = defaultFields.map((field) => {
128132
if ('name' in field && field.type === 'group' && field.name === 'payment') {

test/plugin-form-builder/e2e.spec.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { fileURLToPath } from 'url'
77
import type { PayloadTestSDK } from '../helpers/sdk/index.js'
88
import type { Config } from './payload-types.js'
99

10-
import { ensureCompilationIsDone, initPageConsoleErrorCatch } from '../helpers.js'
10+
import { ensureCompilationIsDone, initPageConsoleErrorCatch, saveDocAndAssert } from '../helpers.js'
1111
import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
1212
import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js'
1313
import { POLL_TOPASS_TIMEOUT, TEST_TIMEOUT_LONG } from '../playwright.config.js'
@@ -42,10 +42,6 @@ test.describe('Form Builder Plugin', () => {
4242
test('has contact form', async () => {
4343
await page.goto(formsUrl.list)
4444

45-
await expect(() => expect(page.url()).toContain('forms')).toPass({
46-
timeout: POLL_TOPASS_TIMEOUT,
47-
})
48-
4945
const titleCell = page.locator('.row-2 .cell-title a')
5046
await expect(titleCell).toHaveText('Contact Form')
5147
const href = await titleCell.getAttribute('href')
@@ -88,10 +84,6 @@ test.describe('Form Builder Plugin', () => {
8884
test('has form submissions', async () => {
8985
await page.goto(submissionsUrl.list)
9086

91-
await expect(() => expect(page.url()).toContain('form-submissions')).toPass({
92-
timeout: POLL_TOPASS_TIMEOUT,
93-
})
94-
9587
const firstSubmissionCell = page.locator('.table .cell-id a').last()
9688
const href = await firstSubmissionCell.getAttribute('href')
9789

@@ -135,14 +127,30 @@ test.describe('Form Builder Plugin', () => {
135127

136128
await page.goto(submissionsUrl.edit(createdSubmission.id))
137129

138-
await expect(() => expect(page.url()).toContain(createdSubmission.id)).toPass({
139-
timeout: POLL_TOPASS_TIMEOUT,
140-
})
141-
142130
await expect(page.locator('#field-submissionData__0__value')).toHaveValue('New tester')
143131
await expect(page.locator('#field-submissionData__1__value')).toHaveValue('new@example.com')
144132
})
145133

134+
test('can create form submission from the admin panel', async () => {
135+
await page.goto(submissionsUrl.create)
136+
await page.locator('#field-form').click({ delay: 100 })
137+
const options = page.locator('.rs__option')
138+
await options.locator('text=Contact Form').click()
139+
140+
await expect(page.locator('#field-form').locator('.rs__value-container')).toContainText(
141+
'Contact Form',
142+
)
143+
144+
await page.locator('#field-submissionData button.array-field__add-row').click()
145+
await page.locator('#field-submissionData__0__field').fill('name')
146+
await page.locator('#field-submissionData__0__value').fill('Test Submission')
147+
await saveDocAndAssert(page)
148+
149+
// Check that the fields are still editable, as this user is an admin
150+
await expect(page.locator('#field-submissionData__0__field')).toBeEditable()
151+
await expect(page.locator('#field-submissionData__0__value')).toBeEditable()
152+
})
153+
146154
test('can create form submission - with date field', async () => {
147155
const { docs } = await payload.find({
148156
collection: 'forms',
@@ -176,10 +184,6 @@ test.describe('Form Builder Plugin', () => {
176184

177185
await page.goto(submissionsUrl.edit(createdSubmission.id))
178186

179-
await expect(() => expect(page.url()).toContain(createdSubmission.id)).toPass({
180-
timeout: POLL_TOPASS_TIMEOUT,
181-
})
182-
183187
await expect(page.locator('#field-submissionData__0__value')).toHaveValue('New tester')
184188
await expect(page.locator('#field-submissionData__1__value')).toHaveValue('new@example.com')
185189
await expect(page.locator('#field-submissionData__2__value')).toHaveValue(

test/plugin-form-builder/payload-types.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export interface Form {
171171
root: {
172172
type: string;
173173
children: {
174-
type: string;
174+
type: any;
175175
version: number;
176176
[k: string]: unknown;
177177
}[];
@@ -295,7 +295,7 @@ export interface Form {
295295
root: {
296296
type: string;
297297
children: {
298-
type: string;
298+
type: any;
299299
version: number;
300300
[k: string]: unknown;
301301
}[];
@@ -332,7 +332,7 @@ export interface Form {
332332
root: {
333333
type: string;
334334
children: {
335-
type: string;
335+
type: any;
336336
version: number;
337337
[k: string]: unknown;
338338
}[];
@@ -356,6 +356,7 @@ export interface Form {
356356
*/
357357
export interface User {
358358
id: string;
359+
roles?: 'admin'[] | null;
359360
updatedAt: string;
360361
createdAt: string;
361362
email: string;
@@ -365,6 +366,13 @@ export interface User {
365366
hash?: string | null;
366367
loginAttempts?: number | null;
367368
lockUntil?: string | null;
369+
sessions?:
370+
| {
371+
id: string;
372+
createdAt?: string | null;
373+
expiresAt: string;
374+
}[]
375+
| null;
368376
password?: string | null;
369377
}
370378
/**
@@ -481,6 +489,7 @@ export interface PagesSelect<T extends boolean = true> {
481489
* via the `definition` "users_select".
482490
*/
483491
export interface UsersSelect<T extends boolean = true> {
492+
roles?: T;
484493
updatedAt?: T;
485494
createdAt?: T;
486495
email?: T;
@@ -490,6 +499,13 @@ export interface UsersSelect<T extends boolean = true> {
490499
hash?: T;
491500
loginAttempts?: T;
492501
lockUntil?: T;
502+
sessions?:
503+
| T
504+
| {
505+
id?: T;
506+
createdAt?: T;
507+
expiresAt?: T;
508+
};
493509
}
494510
/**
495511
* This interface was referenced by `Config`'s JSON-Schema

0 commit comments

Comments
 (0)