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
+12 -8
View File
@@ -12,11 +12,13 @@ export const NOTIFICATION_FALLBACK: NotificationTexts = {
back: 'Back',
consent: 'Accept the privacy policy to send the form.',
error: 'The form could not be sent. Please try again in a moment.',
fileCount: 'Attach at most {max} files in "{field}".',
fileNotSent: 'We could not send your file. Try again, or send it by e-mail.',
fileCount: 'Too many files in "{field}". Remove some: the limit is {max}.',
fileNotSent: 'We could not send the file "{file}". Choose a smaller file or another format and try again.',
fileOptional: 'You can also send the form without the file.',
fileRefused: 'The file "{file}" was refused: it is a program or an archive, it holds macros, or its content does not match its type. Choose another file ({types}).',
fileRemove: 'Remove {file}',
fileTooLarge: '"{file}" is too large. The limit is {max} MB.',
fileType: '"{file}" is not an accepted file type. Accepted: {types}.',
fileTooLarge: 'The file "{file}" is too large. Choose a file of up to {max} MB.',
fileType: 'The file "{file}" is in a format we do not accept. Choose one of: {types}.',
next: 'Next',
notFound: 'This form is no longer available.',
rateLimited: 'Too many attempts. Please wait a moment and try again.',
@@ -34,11 +36,13 @@ export const NOTIFICATION_FALLBACK_PL: NotificationTexts = {
back: 'Wstecz',
consent: 'Zaakceptuj politykę prywatności, aby wysłać formularz.',
error: 'Nie udało się wysłać formularza. Spróbuj ponownie za chwilę.',
fileCount: 'W polu „{field}” można dodać najwyżej {max} plików.',
fileNotSent: 'Nie udało się wysłać pliku. Spróbuj ponownie lub wyślij go e-mailem.',
fileCount: 'Za dużo plików w polu „{field}”. Usuń część: limit to {max}.',
fileNotSent: 'Nie udało się wysłać pliku „{file}”. Wybierz mniejszy plik lub inny format i spróbuj ponownie.',
fileOptional: 'Możesz też wysłać formularz bez pliku.',
fileRefused: 'Plik „{file}” został odrzucony: to program lub archiwum, zawiera makra albo jego zawartość nie odpowiada typowi. Wybierz inny plik ({types}).',
fileRemove: 'Usuń {file}',
fileTooLarge: 'Plik „{file}” jest za duży. Limit to {max} MB.',
fileType: 'Plik „{file}” ma niedozwolony typ. Dozwolone: {types}.',
fileTooLarge: 'Plik „{file}” jest za duży. Wybierz plik do {max} MB.',
fileType: 'Plik „{file}” ma format, którego nie przyjmujemy. Wybierz jeden z: {types}.',
next: 'Dalej',
notFound: 'Ten formularz nie jest już dostępny.',
rateLimited: 'Zbyt wiele prób. Odczekaj chwilę i spróbuj ponownie.',
@@ -34,6 +34,8 @@ export async function getNotificationTexts({
error: f.error || fb.error,
fileCount: f.fileCount || fb.fileCount,
fileNotSent: f.fileNotSent || fb.fileNotSent,
fileOptional: f.fileOptional || fb.fileOptional,
fileRefused: f.fileRefused || fb.fileRefused,
fileRemove: f.fileRemove || fb.fileRemove,
fileTooLarge: f.fileTooLarge || fb.fileTooLarge,
fileType: f.fileType || fb.fileType,
+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
}
+8 -1
View File
@@ -12,8 +12,15 @@ export type FormNotificationTexts = {
error: string
/** File field: too many files, with {field} and {max}. */
fileCount: string
/** File field: the e-mail with the file could not be sent — the visitor has to send it again. */
/** File field: the e-mail with the file could not be sent — with {file}; the visitor has to send it again. */
fileNotSent: string
/** File field: added to a file message when the field is not required ("…send the form without the file"). */
fileOptional: string
/**
* File field: the format is allowed but this file is not — its content does not match, it has macros, it is
* a program or an archive. With {file} and {types}.
*/
fileRefused: string
/** File field: the button that removes a selected file, with {file}. */
fileRemove: string
/** File field: a file over the limit, with {file} and {max} (MB). */