Forms: conditional fields and steps (Show only when)
Every form block (built-in, Checkbox group, Step, message) gets a showWhen group: visible only when another field has one of the given values. A hidden Step drops out with its fields; browser and server share visibleFields, so a hidden field is neither required nor stored. GDPR consent is always enforced. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import type { FieldCondition } from './builderTypes.js'
|
||||
|
||||
import { visibleFields } from './builderLogic.js'
|
||||
|
||||
/** A form-builder field, trimmed to what validation needs. */
|
||||
type FormField = {
|
||||
blockType?: string
|
||||
@@ -11,6 +15,8 @@ type FormField = {
|
||||
/** select / radio / checkboxGroup choices defined in the panel. */
|
||||
options?: { value?: unknown }[] | null
|
||||
required?: boolean | null
|
||||
/** "Show only when" — hidden fields are neither required nor stored. */
|
||||
showWhen?: FieldCondition | null
|
||||
}
|
||||
|
||||
type FormDoc = {
|
||||
@@ -171,10 +177,15 @@ export async function validateSubmission(
|
||||
typeof f.name === 'string' && !NON_DATA_BLOCKS.has(f.blockType ?? ''),
|
||||
)
|
||||
const known = new Map(fields.map((f) => [f.name, f]))
|
||||
// Same rule as the browser: what a failed "Show only when" hid wasn't on screen.
|
||||
const shown = new Set(visibleFields(form.fields, data))
|
||||
|
||||
const cleaned: Record<string, unknown> = {}
|
||||
|
||||
for (const field of fields) {
|
||||
// Hidden by a condition: not required, not stored. The consent gate never hides —
|
||||
// an editor can't switch GDPR consent off with a condition.
|
||||
if (!shown.has(field) && field.name !== consentFieldName) {continue}
|
||||
const value = data[field.name]
|
||||
const isBlank =
|
||||
value == null ||
|
||||
|
||||
Reference in New Issue
Block a user