Skip to content

Beautiful Open-Source Form Builder

Fix public form crashes with empty date range mentions (#1392)

Julien Nahum9b930d4846b2
diff --git a/client/components/forms/components/FormSubmissionFormatter.js b/client/components/forms/components/FormSubmissionFormatter.js
index 1a1f9228c..a4fd6a196 100644
--- a/client/components/forms/components/FormSubmissionFormatter.js
+++ b/client/components/forms/components/FormSubmissionFormatter.js
@@ -1,4 +1,4 @@
-import { format, parseISO } from 'date-fns'
+import { format, isValid, parseISO } from 'date-fns'
// Singleton cache for formatter instances (keyed by form slug)
const formatterCache = new Map()
@@ -118,13 +118,17 @@ export class FormSubmissionFormatter {
const timeFormat = field.with_time ? (field.time_format === 24 ? 'HH:mm' : 'h:mm a') : ''
const fullFormat = `${dateFormat}${timeFormat ? ' ' + timeFormat : ''}`
+ const formatDate = (date) => {
+ if (typeof date !== 'string' || !date) return ''
+ const parsedDate = parseISO(date)
+ return isValid(parsedDate) ? format(parsedDate, fullFormat) : ''
+ }
+
if (Array.isArray(value)) {
- const start = format(parseISO(value[0]), fullFormat)
- const end = value[1] ? format(parseISO(value[1]), fullFormat) : null
- return end ? `${start} - ${end}` : start
+ return value.slice(0, 2).map(formatDate).filter(Boolean).join(' - ')
}
- return format(parseISO(value), fullFormat)
+ return formatDate(value)
}
formatPeopleValue(value) {
diff --git a/client/test/e2e/form-initialization-ssr.spec.ts b/client/test/e2e/form-initialization-ssr.spec.ts
index bbe74eb14..6acae4cae 100644
--- a/client/test/e2e/form-initialization-ssr.spec.ts
+++ b/client/test/e2e/form-initialization-ssr.spec.ts
@@ -39,6 +39,23 @@ async function createForm(request: APIRequestContext, properties: unknown[], ove
return (await response.json()).form
}
+test('field mentions render an empty prefilled date range without crashing the public form', async ({ page, request }) => {
+ const form = await createForm(request, [
+ { id: 'summary', name: 'Summary', type: 'nf-text',
+ content: '<p>Travel dates: <span mention="true" mention-field-id="dates" mention-field-name="Dates" mention-fallback="Not selected">Dates</span></p>' },
+ { id: 'name', name: 'Full name', type: 'text', required: true },
+ { id: 'dates', name: 'Dates', type: 'date', date_range: true, prefill: [null, null] },
+ ])
+
+ await page.goto(`/forms/${form.slug}`)
+
+ await expect(page.getByLabel('Full name', { exact: true })).toBeVisible()
+ await expect(page.getByText('Travel dates: Not selected', { exact: true })).toBeVisible()
+ await page.getByLabel('Full name', { exact: true }).fill('Alex')
+ await expect(page.getByLabel('Full name', { exact: true })).toHaveValue('Alex')
+ await expect(page.locator('body')).not.toContainText('Whoops, something went wrong')
+})
+
function collectHydrationWarnings(page: Page) {
const warnings: string[] = []
page.on('console', message => {
diff --git a/client/test/unit/FormSubmissionFormatter.test.ts b/client/test/unit/FormSubmissionFormatter.test.ts
index dc09c9e37..0d7b7b6ff 100644
--- a/client/test/unit/FormSubmissionFormatter.test.ts
+++ b/client/test/unit/FormSubmissionFormatter.test.ts
@@ -340,6 +340,38 @@ describe('Date Formatting', () => {
expect(result.date).toBe('15/01/2024 - 20/01/2024')
})
+ it.each([
+ [], [null, null], [undefined, undefined], ['', ''], ['invalid', '2024-02-30'],
+ ])('formats an empty or invalid date range %j as empty text', (...dates) => {
+ const formatter = new FormSubmissionFormatter(createDateForm(), { date: dates })
+
+ expect(formatter.setOutputStringsOnly().getFormattedData().date).toBe('')
+ })
+
+ it.each([
+ [['2024-01-15', null], '15/01/2024'],
+ [[null, '2024-01-20'], '20/01/2024'],
+ [['2024-01-15', 'invalid'], '15/01/2024'],
+ ])('formats the valid endpoint of a partial date range %j', (date, expected) => {
+ const formatter = new FormSubmissionFormatter(createDateForm(), { date })
+
+ expect(formatter.getFormattedData().date).toBe(expected)
+ })
+
+ it.each(['invalid', '2024-02-30', {}, 123])('formats an invalid single date %j as empty text', date => {
+ const formatter = new FormSubmissionFormatter(createDateForm(), { date })
+
+ expect(formatter.getFormattedData().date).toBe('')
+ })
+
+ it('keeps other answers available when a date range is empty', () => {
+ const form = createDateForm()
+ form.properties.push({ id: 'name', name: 'Name', type: 'text' })
+ const formatter = new FormSubmissionFormatter(form, { date: [null, null], name: 'Alex' })
+
+ expect(formatter.getFormattedData()).toEqual({ date: '', name: 'Alex' })
+ })
+
it('should return ISO format when useIsoFormat enabled', () => {
const form = createDateForm()
const formatter = new FormSubmissionFormatter(form, { date: '2024-01-15' })