Form files: the visitor is told when the problem is the file — why and what to do; UTF-8 file names

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-13 16:51:05 +01:00
co-authored by Claude Fable 5.2
parent b6f9f34705
commit 97202ee03e
18 changed files with 335 additions and 128 deletions
+26 -11
View File
@@ -5,10 +5,16 @@ import type { FormNotificationTexts } from './types.js'
* The texts resolveFormMessage needs. The File-field texts are optional, so an
* object written before they existed still fits (the field or general text is used).
*/
export type FormMessageTexts = { [K in 'fileCount' | 'fileNotSent' | 'fileRemove' | 'fileTooLarge' | 'fileType']?: string } & Omit<
FormNotificationTexts,
'fileCount' | 'fileNotSent' | 'fileRemove' | 'fileTooLarge' | 'fileType'
>
type FileTextKey = 'fileCount' | 'fileNotSent' | 'fileOptional' | 'fileRefused' | 'fileRemove' | 'fileTooLarge' | 'fileType'
export type FormMessageTexts = { [K in FileTextKey]?: string } & Omit<FormNotificationTexts, FileTextKey>
/** The texts of the File field problems, by result code. */
const FILE_TEXT: Partial<Record<string, FileTextKey>> = {
file_count: 'fileCount',
file_refused: 'fileRefused',
file_too_large: 'fileTooLarge',
file_type: 'fileType',
}
/**
* Maps a submitForm result to the user-facing message, interpolating {field}
@@ -27,8 +33,11 @@ export function resolveFormMessage(result: SubmitFormResult, texts: FormMessageT
switch (result.reason) {
case 'consent':
return texts.consent
case 'delivery':
return texts.fileNotSent ?? texts.error
case 'delivery': {
// The file could not be e-mailed: say it is the file (at its field), never the mail server's own words.
if (!texts.fileNotSent) {return texts.error}
return withoutFile(texts.fileNotSent.replace('{file}', result.file ?? ''), result.optional, texts)
}
case 'not_found':
return texts.notFound
case 'rate_limited':
@@ -38,15 +47,16 @@ export function resolveFormMessage(result: SubmitFormResult, texts: FormMessageT
case 'validation': {
// {field} → the label the visitor sees; no single field at fault → the generic error.
const field = result.label || result.field
const fill = (text: string) =>
text
// A problem with a file says so: which file, why, and what to do next.
const fileText = texts[FILE_TEXT[result.kind ?? ''] ?? 'fileRemove']
if (FILE_TEXT[result.kind ?? ''] && fileText) {
const message = fileText
.replace('{file}', result.file ?? '')
.replace('{max}', result.max === undefined ? '' : String(result.max))
.replace('{types}', result.types ?? '')
.replace('{field}', field ?? '')
if (result.kind === 'file_too_large' && texts.fileTooLarge) {return fill(texts.fileTooLarge)}
if (result.kind === 'file_type' && texts.fileType) {return fill(texts.fileType)}
if (result.kind === 'file_count' && texts.fileCount) {return fill(texts.fileCount)}
return withoutFile(message, result.optional, texts)
}
return field ? texts.validation.replace('{field}', field) : texts.error
}
case 'error':
@@ -54,3 +64,8 @@ export function resolveFormMessage(result: SubmitFormResult, texts: FormMessageT
return texts.error
}
}
/** On a field that is not required, a file message ends with "…send the form without the file". */
function withoutFile(message: string, optional: boolean | undefined, texts: FormMessageTexts): string {
return optional && texts.fileOptional ? `${message} ${texts.fileOptional}` : message
}