WIP: form attachments docs (forms, email, security, storage, deployment, notifications), honeypot value sent

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-13 16:30:44 +01:00
co-authored by Claude Fable 5.2
parent 701bb420c1
commit 344574d6bd
8 changed files with 335 additions and 16 deletions
+1 -2
View File
@@ -9,7 +9,6 @@ import type { BuilderField } from './builderTypes.js'
import type { BuilderFormApi, UseBuilderFormOptions } from './useBuilderForm.js'
import { formatFileSize } from './attachments/fileTypes.js'
import { honeypotInputProps } from './honeypot.js'
import { useBuilderForm } from './useBuilderForm.js'
/**
@@ -228,7 +227,7 @@ export function BuilderForm({
return (
<form className={cn.form} data-ipal="form" data-step={f.stepIndex + 1} noValidate onSubmit={f.submit}>
<input {...honeypotInputProps()} />
<input {...f.getHoneypotProps()} />
{f.total > 1 && progress ? <p aria-live="polite" className={cn.progress}>{progress}</p> : null}
<div className={cn.step} data-ipal="form-step" ref={stepRef} tabIndex={-1}>
{step.title ? <p className={cn.stepTitle}>{step.title}</p> : null}
+19 -1
View File
@@ -10,6 +10,7 @@ import { safeHref } from '../links/resolveLinkHref.js'
import { useOptionalTurnstile } from '../turnstile/TurnstileProvider.js'
import { autoCompleteFor, fileFieldError, fileFieldRules, initialBuilderValues, inputFields, splitSteps, stepOfField, validateBuilderFields, visibleFields } from './builderLogic.js'
import { formAnalyticsOnSuccess } from './formAnalytics.js'
import { HONEYPOT_FIELD, honeypotInputProps } from './honeypot.js'
import { useForm } from './useForm.js'
export type UseBuilderFormOptions = {
@@ -67,12 +68,17 @@ export function useBuilderForm({ action, fileSubmitUrl, form, locale, navigate,
// browser or a proxy) carries the same key and the server sends nothing twice.
// A new key only once the server has answered.
const attemptKey = useRef('')
// The honeypot input is not part of the form values; its content is read at submit
// and sent along, so the server can drop a bot that filled it.
const honeypot = useRef<HTMLInputElement | null>(null)
const submitAction = useCallback(
async (values: Record<string, unknown>, token?: string) => {
const shownFields = visibleInputs(values)
const shown = new Set(shownFields.map((field) => field.name))
const fileNames = new Set(shownFields.filter((field) => field.blockType === 'file').map((field) => field.name))
const answers = Object.fromEntries(Object.entries(values).filter(([name]) => shown.has(name) && !fileNames.has(name)))
const answers: Record<string, unknown> = Object.fromEntries(Object.entries(values).filter(([name]) => shown.has(name) && !fileNames.has(name)))
const trap = honeypot.current
if (trap?.value) {answers[trap.name || HONEYPOT_FIELD] = trap.value}
const files = [...fileNames].flatMap((name) => (Array.isArray(values[name]) ? (values[name] as File[]) : []).map((file) => ({ name, file })))
let result: Awaited<ReturnType<BuilderFormAction>>
@@ -276,6 +282,17 @@ export function useBuilderForm({ action, fileSubmitUrl, form, locale, navigate,
[f, getFieldProps, textValue],
)
/** Props of the hidden honeypot input — render one `<input {...f.getHoneypotProps()} />` inside the form. */
const getHoneypotProps = useCallback(
(name?: string) => ({
...honeypotInputProps(name),
ref: (input: HTMLInputElement | null) => {
honeypot.current = input
},
}),
[],
)
const helpId = useCallback((field: BuilderField) => `${fieldId(field)}-help`, [fieldId])
// File fields: the selected files live in the form values (File[]); the native
@@ -390,6 +407,7 @@ export function useBuilderForm({ action, fileSubmitUrl, form, locale, navigate,
getFieldProps,
getFileProps,
getFiles,
getHoneypotProps,
getInputProps,
getOptionProps,
getSelectProps,