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:
co-authored by
Claude Fable 5.2
parent
b6f9f34705
commit
97202ee03e
@@ -131,22 +131,25 @@ export function fileFieldRules(field: BuilderField): Required<BuilderFileLimits>
|
||||
export function fileFieldError(
|
||||
field: BuilderField,
|
||||
files: readonly BuilderFile[],
|
||||
texts: Pick<BuilderFormTexts, 'fileCount' | 'fileTooLarge' | 'fileType' | 'validation'>,
|
||||
texts: Pick<BuilderFormTexts, 'fileCount' | 'fileOptional' | 'fileTooLarge' | 'fileType' | 'validation'>,
|
||||
): string | undefined {
|
||||
const rules = fileFieldRules(field)
|
||||
const label = field.label || field.name || ''
|
||||
const generic = texts.validation.replace('{field}', label)
|
||||
if (files.length > rules.maxFiles) {return (texts.fileCount ?? generic).replace('{field}', label).replace('{max}', String(rules.maxFiles))}
|
||||
// On a field that is not required the visitor may also send the form without the file — say so.
|
||||
const say = (text: string | undefined, fill: (text: string) => string) =>
|
||||
text ? `${fill(text)}${!field.required && texts.fileOptional ? ` ${texts.fileOptional}` : ''}` : generic
|
||||
if (files.length > rules.maxFiles) {return say(texts.fileCount, (t) => t.replace('{field}', label).replace('{max}', String(rules.maxFiles)))}
|
||||
for (const file of files) {
|
||||
if (rules.extensions.length > 0 && !rules.extensions.includes(fileExtensions(file.name).at(-1) ?? '')) {
|
||||
return (texts.fileType ?? generic).replace('{file}', file.name).replace('{types}', rules.typeLabels)
|
||||
return say(texts.fileType, (t) => t.replace('{file}', file.name).replace('{types}', rules.typeLabels))
|
||||
}
|
||||
if (file.size > rules.maxSizeMB * MB || file.size === 0) {
|
||||
return (texts.fileTooLarge ?? generic).replace('{file}', file.name).replace('{max}', String(rules.maxSizeMB))
|
||||
return say(texts.fileTooLarge, (t) => t.replace('{file}', file.name).replace('{max}', String(rules.maxSizeMB)))
|
||||
}
|
||||
}
|
||||
if (files.reduce((sum, f) => sum + f.size, 0) > rules.maxTotalMB * MB) {
|
||||
return (texts.fileTooLarge ?? generic).replace('{file}', files.at(-1)?.name ?? '').replace('{max}', String(rules.maxTotalMB))
|
||||
return say(texts.fileTooLarge, (t) => t.replace('{file}', files.at(-1)?.name ?? '').replace('{max}', String(rules.maxTotalMB)))
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
@@ -170,7 +173,7 @@ const isBlank = isBlankValue
|
||||
export function validateBuilderFields(
|
||||
fields: BuilderField[],
|
||||
values: Record<string, unknown>,
|
||||
texts: Pick<BuilderFormTexts, 'fileCount' | 'fileTooLarge' | 'fileType' | 'validation'>,
|
||||
texts: Pick<BuilderFormTexts, 'fileCount' | 'fileOptional' | 'fileTooLarge' | 'fileType' | 'validation'>,
|
||||
): Record<string, string> {
|
||||
const errors: Record<string, string> = {}
|
||||
for (const f of inputFields(fields)) {
|
||||
|
||||
Reference in New Issue
Block a user