Skip to content
16 changes: 16 additions & 0 deletions .changeset/30220-requiredwhen-formpage-kanban.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@object-ui/console': patch
'@object-ui/plugin-kanban': patch
'@object-ui/plugin-form': patch
---

fix(ui): the console form page Edit, the kanban drop prompt and the Edit wizard judge a field's `requiredWhen` as the engine does, a transition gate against the stored record

Clause-②: no
Metadata-protocol: none

- **`@object-ui/console` form page (`/forms/:name?recordId=`):** an Edit no longer refuses a record that already sits inside a `requiredWhen` gate without the value (for example a contract activated before its signed-copy rule existed). The field shows no required marker there, and an unrelated edit saves, as REST `PATCH` already accepts. Still refused: the edit that moves a record into the gate without the value, and clearing the value of a record that held it. A create form enforces as before.
- **`@object-ui/plugin-kanban` drop prompt:** dropping a card no longer asks for a field the engine lets rest. A record already inside the gate without the value moves between columns with no dialog; a drop that moves a compliant record into the gate still asks for the value first.
- **`@object-ui/plugin-form` Edit wizard:** the final submit's cross-step check no longer refuses a record that already sits inside a `requiredWhen` gate without the value; it asks `requiredWhenRestsOnStoredRecord` about the record as first read. The record reaches only that stored-record check, so a `visibleWhen` reading `previous` is still unevaluable at that check, as before. Moving a compliant record into the gate is still refused, and a Create wizard enforces as before.

All three read the stored-record verdict `resolveFieldRuleState` already returns (`requiredWhenAtRest`, or its exported check `requiredWhenRestsOnStoredRecord`); no second rule.
75 changes: 75 additions & 0 deletions ui/apps/console/src/components/FormPage.conditionalRules.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,78 @@ describe('objectui#5627 — the keys reach the rows, in their own slots', () =>
expect(resolveRowState(byName.owned, values, {}, false).required).toBe(true);
});
});

// ─── 5. requiredWhen on an EDIT is the engine's transition gate ──────────

/**
* objectstack#30220 — an Edit through `/forms/:name?recordId=` judges
* `requiredWhen` as objectql does: an update is refused only when the merged
* record violates the rule AND the stored row complied (ADR-0113
* non-regression). `resolveRowState` hands `resolveFieldRuleState` the stored
* row AND the field's column `name`, so a record already inside the gate
* without the value saves an unrelated edit. The CONTROL is the gate still
* holding: clearing the value of a record that complied is refused.
*/
describe('objectstack#30220 — FormPage judges requiredWhen on an Edit as a transition gate', () => {
const SIGNED_WHEN_ACTIVATED = withRules({
notes: { label: 'Notes', requiredWhen: "record.status == 'activated'" },
});
const SECTIONS = [{ label: 'Basics', fields: ['priority', 'title', 'status', 'notes'] }];
const writes = () => calls.filter((c) => c.method === 'POST' || c.method === 'PATCH');

function renderEdit(stored: Record<string, unknown>) {
return renderInternal(SECTIONS, {
objectSchema: SIGNED_WHEN_ACTIVATED,
query: '?recordId=c-1',
extraRoutes: [
{
match: '/data/showcase_task/c-1',
body: { object: 'showcase_task', id: 'c-1', record: { id: 'c-1', ...stored } },
},
{
method: 'PATCH',
match: '/data/showcase_task/c-1',
body: { object: 'showcase_task', id: 'c-1', record: { id: 'c-1', ...stored } },
},
],
});
}

it('saves an unrelated edit to a record already inside the gate without the value', async () => {
const user = userEvent.setup();
const { container } = renderEdit({ priority: 'low', title: 'Old', status: 'activated' });
await awaitForm();
// At rest: neither the marker nor the control says required.
expect(controlFor(container, 'notes')).not.toBeRequired();
expect(labelFor(container, 'notes').textContent).not.toContain('*');

const title = screen.getByLabelText('Title');
await user.clear(title);
await user.type(title, 'Renamed');
await user.click(screen.getByRole('button', { name: /submit/i }));
await waitFor(() => expect(writes()).toHaveLength(1));
expect(writes()[0].method).toBe('PATCH');
expect((writes()[0].body as Record<string, unknown>).title).toBe('Renamed');
});

it('CONTROL: clearing the value of a record that complied is still refused', async () => {
const { toast } = await import('sonner');
vi.mocked(toast.error).mockClear();
const user = userEvent.setup();
const { container } = renderEdit({
priority: 'low',
title: 'Old',
status: 'activated',
notes: 'signed.pdf',
});
await awaitForm();
expect(controlFor(container, 'notes')).toBeRequired();

await user.clear(screen.getByLabelText(/^Notes/));
expect(controlFor(container, 'notes')).toBeRequired();
expect(labelFor(container, 'notes').textContent).toContain('*');
await user.click(screen.getByRole('button', { name: /submit/i }));
await waitFor(() => expect(vi.mocked(toast.error)).toHaveBeenCalled());
expect(writes()).toHaveLength(0);
});
});
7 changes: 7 additions & 0 deletions ui/apps/console/src/components/FormPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,13 @@ export function resolveRowState(
{ field: { defaultValue: field.defaultValue } },
isCreateForm,
),
// The column of `previous` that holds this field's stored value, so an
// EDIT judges `requiredWhen` as the engine's transition gate: a stored
// row already inside the gate without the value rests there, and an
// unrelated edit saves (objectstack#30220, the verdict #30184 put in
// `resolveFieldRuleState`). A create form passes no `previous`, so it
// keeps the plain verdict.
name: field.name,
},
previous ?? undefined,
predicateScope,
Expand Down
41 changes: 39 additions & 2 deletions ui/packages/plugin-form/src/WizardForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import React, { useState, useCallback, useMemo } from 'react';
import type { FormField, FormSchema, DataSource, ObjectFormSchema, ObjectFormSection } from '@object-ui/types';
import { Button, cn, toast } from '@object-ui/components';
import { AlertCircle, Check, ChevronLeft, ChevronRight, Loader2 } from 'lucide-react';
import { resolveFieldRuleState, evalFieldPredicate, isMissingForRequired, isServerOwnedValue } from '@object-ui/core';
import {
resolveFieldRuleState,
evalFieldPredicate,
isMissingForRequired,
isServerOwnedValue,
requiredWhenRestsOnStoredRecord,
} from '@object-ui/core';
import { createSafeTranslation, useLocalization } from '@object-ui/i18n';
import { FormSectionContainer } from './FormSection';
import { SchemaRenderer, useSafeFieldLabel, usePredicateScope } from '@object-ui/react';
Expand Down Expand Up @@ -772,6 +778,9 @@ export const WizardForm: React.FC<WizardFormProps> = ({
// `required` suppression use, so this wizard cannot be seeded as a create
// form and gated as an edit one.
const isCreateWizard = isCreateFormMode(schema);
// The edit test the step forms already use to hand `persistedRecord` on as
// `previousValues`; the final gate's stored-row check reads the same one.
const isEditWizard = schema.mode === 'edit';

/**
* The host shell's global predicate scope (`ExpressionProvider` →
Expand Down Expand Up @@ -838,6 +847,20 @@ export const WizardForm: React.FC<WizardFormProps> = ({
* half is the same mechanism, accepted as a residual under the same refusal
* of option D on objectui#8069 — ⛔ so `persistedRecord` is deliberately NOT
* bound here.
*
* ## `requiredWhen` is a transition gate on an EDIT wizard (objectstack#30220)
*
* The engine refuses an update only when the merged record violates
* `requiredWhen` AND the stored row complied (ADR-0113 non-regression). So on
* an EDIT wizard a field the live verdict calls required is skipped when the
* stored row already rests inside its gate, as `requiredWhenRestsOnStoredRecord`
* answers over `persistedRecord` — the same verdict the step forms read. As
* ruled on objectstack#30220 (option A), the persisted record reaches ONLY
* that stored-row check: the live `resolveFieldRuleState` call above still
* binds no `previous`, so the option-D refusal and its `visibleWhen` residual
* stand unchanged. The check can only relax a `requiredWhen`, never a static
* `required`, and reports no fault (an unevaluable predicate counts the stored
* row as compliant, as on the server).
*/
const gateFinalSubmit = useCallback(
(
Expand Down Expand Up @@ -896,12 +919,26 @@ export const WizardForm: React.FC<WizardFormProps> = ({
// A hidden or read-only field is not the user's to fill in.
if (!viewVisible || !state.visible || state.readonly || !state.required) continue;
if (!isEmptyValue(record[name])) continue;
// The stored row already rests inside this field's `requiredWhen`
// gate: the engine lets the write through (see the note above).
if (
isEditWizard &&
!field.required &&
requiredWhenRestsOnStoredRecord(
(field as any).requiredWhen,
name,
persistedRecord,
predicateScope,
)
) {
continue;
}
out.set(index, [...(out.get(index) ?? []), (field as any).label || name]);
}
});
return { missing: out, faultedVisibleWhen };
},
[schema.sections, buildSectionFields, isCreateWizard, predicateScope],
[schema.sections, buildSectionFields, isCreateWizard, isEditWizard, persistedRecord, predicateScope],
);

// Handle step data collection (merge partial data into formData)
Expand Down
78 changes: 78 additions & 0 deletions ui/packages/plugin-form/src/wizardSkipValidation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,81 @@ describe('WizardForm — the form view’s own `columns`', () => {
expect(document.body.querySelector('[class*="@2xl:grid-cols-3"]')).toBeNull();
});
});

/**
* objectstack#30220 (ruled option A) — on an EDIT wizard the cross-step gate
* judges `requiredWhen` as the engine does: a TRANSITION gate. objectql
* refuses an update only when the merged record violates the rule AND the
* stored row complied (ADR-0113 non-regression), so a record already inside
* the gate without the value saves an unrelated edit. The field sits on the
* step the user skips, so only the cross-step gate judges it. The CONTROL is a
* record that complied and is moved into the gate: still refused.
*/
describe('WizardForm edit — the final gate reads requiredWhen as a transition gate (objectstack#30220)', () => {
const CONTRACT_SCHEMA = {
name: 'contract',
fields: {
subject: { type: 'text', label: 'Subject' },
status: { type: 'text', label: 'Status' },
signed_copy: { type: 'text', label: 'Signed Copy', requiredWhen: "record.status == 'activated'" },
notes: { type: 'text', label: 'Notes' },
},
};

const editWizard = (stored: Record<string, unknown>) => {
const dataSource = {
getObjectSchema: vi.fn().mockResolvedValue(CONTRACT_SCHEMA),
findOne: vi.fn().mockResolvedValue({ id: 'c1', ...stored }),
create: vi.fn(),
update: vi.fn(async (_o: string, _id: string, d: any) => ({ id: 'c1', ...stored, ...d })),
};
render(
<WizardForm
schema={{
type: 'object-form',
formType: 'wizard',
objectName: 'contract',
mode: 'edit',
recordId: 'c1',
allowSkip: true,
sections: [
{ name: 's1', label: 'Step 1', fields: ['subject', 'status'] },
{ name: 's2', label: 'Step 2', fields: ['signed_copy'] },
{ name: 's3', label: 'Step 3', fields: ['notes'] },
],
}}
dataSource={dataSource as any}
/>,
);
return dataSource;
};

/** Edit step 1 as asked, jump past step 2 to step 3, and press Update. */
async function editAndSubmit(step1: Record<string, string>) {
await waitFor(() => {
const el = document.body.querySelector<HTMLInputElement>('[data-field="subject"] input');
if (!el || el.value !== 'Lease') throw new Error('record not on screen yet');
});
for (const [name, value] of Object.entries(step1)) fill(name, value);
fireEvent.click(stepIndicator(2));
await waitFor(() => expect(document.body.querySelector('[data-field="notes"]')).toBeTruthy());
fill('notes', 'renewed');
expect(document.body.querySelector('[data-field="signed_copy"]')).toBeNull();
fireEvent.click(screen.getByRole('button', { name: /Update/i }));
}

it('saves an unrelated edit to a record already inside the gate without the value', async () => {
const dataSource = editWizard({ subject: 'Lease', status: 'activated', signed_copy: null, notes: '' });
await editAndSubmit({ subject: 'Lease 2026' });
await waitFor(() => expect(dataSource.update).toHaveBeenCalledTimes(1));
expect(dataSource.update.mock.calls[0][2]).toMatchObject({ subject: 'Lease 2026', notes: 'renewed' });
});

it('CONTROL: moving a compliant record into the gate without the value is still refused', async () => {
const dataSource = editWizard({ subject: 'Lease', status: 'draft', signed_copy: null, notes: '' });
await editAndSubmit({ status: 'activated' });
// Nothing is sent, and the wizard lands on the step holding the field.
await waitFor(() => expect(document.body.querySelector('[data-field="signed_copy"]')).toBeTruthy());
expect(dataSource.update).not.toHaveBeenCalled();
});
});
43 changes: 43 additions & 0 deletions ui/packages/plugin-kanban/src/requiredWhenPrompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,46 @@ describe('collectRequiredWhenPromptFields (#4254)', () => {
).toEqual([]);
});
});

/**
* objectstack#30220 — the drop is judged as the engine judges the write: a
* TRANSITION gate. objectql refuses an update only when the merged record
* violates `requiredWhen` AND the stored row complied (ADR-0113
* non-regression), so a record already inside the gate without the value
* moves between columns of an unrelated grouping, and the board must not ask.
* The verdict is `resolveFieldRuleState`'s `requiredWhenAtRest`, read through
* the stored column `name` this module hands it — never a second rule here.
*/
describe('collectRequiredWhenPromptFields judges the transition gate (objectstack#30220)', () => {
const SIGNED_WHEN_ACTIVATED = 'record.status == "activated"';
const contractFields = {
priority: { type: 'picklist', label: 'Priority' },
status: { type: 'picklist', label: 'Status' },
signed_copy: { type: 'text', label: 'Signed Copy', requiredWhen: SIGNED_WHEN_ACTIVATED },
};

it('asks for nothing when the stored record already rests inside the gate without the value', () => {
// An activated contract with no signed copy, regrouped by priority: the
// engine lets this PATCH through, so a prompt would only block the move.
expect(
collectRequiredWhenPromptFields(
contractFields,
{ id: 'c1', priority: 'low', status: 'activated', signed_copy: null },
'priority',
'high',
),
).toEqual([]);
});

it('CONTROL: still asks when the drop moves a compliant record INTO the gate', () => {
// The stored row complied (a draft needs no signed copy) and the drop
// makes it violate: the engine refuses, so the board collects the value.
const out = collectRequiredWhenPromptFields(
contractFields,
{ id: 'c2', priority: 'low', status: 'draft', signed_copy: null },
'status',
'activated',
);
expect(out.map((f) => f.name)).toEqual(['signed_copy']);
});
});
7 changes: 6 additions & 1 deletion ui/packages/plugin-kanban/src/requiredWhenPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ export function collectRequiredWhenPromptFields(
requiredWhen: def.requiredWhen as never,
},
next,
{ required: def.required === true, readonly: def.readonly === true },
// `name` says which column of the pre-drop record is this field's stored
// value, so the drop is judged as the engine judges the write: a
// transition gate (objectstack#30220). A record already inside the gate
// without the value rests there, the engine lets the drop through, and
// the board asks for nothing.
{ required: def.required === true, readonly: def.readonly === true, name },
// The pre-drop record, so a predicate reading `previous.*` sees what the
// form would show it.
record,
Expand Down
Loading