Skip to content

Commit e32d173

Browse files
fix(plugin-form): a create wizard final-submit gate no longer judges the server lock readonlyWhen (#38487)
Fixes #38276 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:在多步骤“新建”向导里,某个必填字段带有“满足条件即锁定”的规则、且新记录的填写值恰好满足该条件时,向导在最后一步会像其它必填字段一样标出“必填”并跳回那一步,不再把空值提交给服务器后才报错;“编辑”向导的行为不变。 - 为什么做:向导最后一步的必填复查与表单本身的规则不一致,前一张同类问题(手动锁定规则)已修过一半,这次补上服务器锁定规则这一半。 - 出错会怎样:只影响新建向导提交前的提示,服务器照旧拒绝缺必填项的新建,不会写入错误数据;有问题可直接撤回。 - 要你做什么:无。 ## What changes `ui/packages/plugin-form/src/WizardForm.tsx`, `gateFinalSubmit`: the field's server lock is now dropped on a CREATE wizard, mirroring the hand-lock line beside it and the form renderer (`form.tsx` passes `readonlyWhen: isCreateForm ? undefined : …` in every rule call): ```ts readonlyWhen: isCreateWizard ? undefined : (field as any).readonlyWhen, ``` Ruling: ADR-0163, "The server lock": "A conditional lock is not judged on a create, which is what makes `readonlyWhen: P\`true\`` set-once." Before this change a `readonlyWhen` TRUE over the create wizard's own values made the gate read the field as read-only and skip its required check, so the create went out with the required field empty. An EDIT wizard still judges the lock. - Server side (triage asked): the insert does enforce `required`. `packages/objectql/src/validation/record-validator.ts` `validateOne`, the `// ── required` arm (`if (!skipRequired && def.required && isEmptyForRequired(def, value) …) return fail('required')`); its header states "on INSERT a missing/null/empty value is rejected". The `readonlyWhen` strip (`stripReadonlyWhenFields` in `rule-validator.ts`) works on UPDATE payloads only. So no wrong row was ever stored; only the client feedback was wrong. - Contract face: none. No spec, schema, export or wire shape moves. Read first: the one-line change in `gateFinalSubmit` (`WizardForm.tsx`), then the new test file. Two-minute check: `pnpm --dir ui exec vitest run packages/plugin-form/src/WizardForm.createServerLock-38276.test.tsx` (expect 2 passed). ## Tests New `ui/packages/plugin-form/src/WizardForm.createServerLock-38276.test.tsx` (3-step wizard with `allowSkip`; the required field sits on the skipped middle step, so only `gateFinalSubmit` can judge it): - a CREATE wizard whose field `readonlyWhen` is TRUE over its values still demands the empty required field (the wizard returns to that step, `create` not called); - CONTROL: an EDIT wizard on a record where `readonlyWhen` holds treats the field as locked and saves without it (`update` called once). <details><summary>Readings at head 9e8bf75</summary> - New file + `wizardPredicateScope.test.tsx`: `Test Files 2 passed (2)`, `Tests 9 passed (9)`. - Whole package `pnpm --dir ui exec vitest run --maxWorkers=2 packages/plugin-form/`: `Test Files 249 passed (249)`, `Tests 2807 passed | 1 skipped (2808)`. - `pnpm --filter @object-ui/plugin-form run type-check` (`tsc --noEmit && tsc -p tsconfig.test.json`): exit 0; `--listFiles` on `tsconfig.test.json` includes the new test file (1 hit). - Ablation through `scripts/ablation-replace.mjs` (anchor hit 1 → 0, blob d81437c → 9a490bfe): with the old line restored, the CREATE pin fails (`Tests 1 failed | 1 passed (2)`) and the EDIT CONTROL stays green; restore verified (blob == HEAD, `git diff HEAD` empty). The test imports `./WizardForm` from source, so no dist rebuild was involved. </details> ## Acceptance notes - None beyond the card. ## Verification Local: the derived gate list (`node scripts/pm/dispatch-gates.mjs --commands`, 38 commands) run at head 9e8bf75; CI runs the full farm and the type-check lanes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01FEkQ5pH8MaBBP5T2dVcTHL Co-authored-by: Claude <noreply@anthropic.com>
1 parent f4d02e6 commit e32d173

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/plugin-form': patch
3+
---
4+
5+
fix(plugin-form): a create wizard's final-submit required check no longer judges a field's server lock `readonlyWhen`
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
On a CREATE wizard the final-submit gate now drops a field's `readonlyWhen`, as the form renderer already does on a create (ADR-0163, "The server lock": a conditional lock is not judged on a create). Before, a `readonlyWhen` that was TRUE over the new record's values made the gate skip that field's required check, so the wizard sent a create with the required field empty and the person met the server's refusal after the last step instead of the field's own required mark. An EDIT wizard still judges the lock: a locked field is not demanded.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* objectstack#38276 — the wizard's submit-time required re-check
11+
* (`gateFinalSubmit`) does not judge a field's own server lock
12+
* (`readonlyWhen`) on a CREATE wizard.
13+
*
14+
* ADR-0163, "The server lock": "A conditional lock is not judged on a create,
15+
* which is what makes `readonlyWhen: P\`true\`` set-once." The form renderer
16+
* drops the predicate on a create; this gate must too, or a lock TRUE over the
17+
* new record's values reads the field as read-only and skips its required
18+
* check, and the empty field reaches the server's insert refusal after the
19+
* last step. On an EDIT wizard the lock still holds: a locked field is not
20+
* demanded.
21+
*
22+
* The required field sits on the MIDDLE step, which the user skips past
23+
* (`allowSkip`), so it never mounts and only `gateFinalSubmit` can speak. The
24+
* observable is whether the gate let the write through (`create` / `update`
25+
* called) or sent the user back to the step holding the field.
26+
*/
27+
28+
import React from 'react';
29+
import { describe, it, expect, vi, afterEach } from 'vitest';
30+
import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react';
31+
import { registerAllFields } from '@object-ui/fields';
32+
import { WizardForm } from './WizardForm';
33+
34+
registerAllFields();
35+
36+
afterEach(() => {
37+
cleanup();
38+
vi.restoreAllMocks();
39+
});
40+
41+
/** True over the create wizard's own values (step 1 fills `subject` with 'S1'). */
42+
const LOCK_TRUE_ON_CREATE = "record.subject == 'S1'";
43+
/** True over the stored record the edit wizard loads. */
44+
const CONFIRMED_LOCK = "record.status == 'confirmed'";
45+
46+
function mount(mode: 'create' | 'edit', readonlyWhen: string, stored?: Record<string, unknown>) {
47+
const dataSource: any = {
48+
getObjectSchema: vi.fn().mockResolvedValue({
49+
name: 'order',
50+
fields: {
51+
subject: { type: 'text', label: 'Subject' },
52+
status: { type: 'text', label: 'Status' },
53+
owner: { type: 'text', label: 'Owner', required: true, readonlyWhen },
54+
notes: { type: 'text', label: 'Notes' },
55+
},
56+
}),
57+
findOne: vi.fn().mockResolvedValue(stored ? { id: 'o1', ...stored } : null),
58+
create: vi.fn().mockResolvedValue({ id: 'o1' }),
59+
update: vi.fn(async (_o: string, _id: string, d: any) => ({ id: 'o1', ...stored, ...d })),
60+
};
61+
render(
62+
<WizardForm
63+
schema={{
64+
type: 'object-form',
65+
formType: 'wizard',
66+
objectName: 'order',
67+
mode,
68+
...(mode === 'edit' ? { recordId: 'o1' } : {}),
69+
allowSkip: true,
70+
sections: [
71+
{ name: 's1', label: 'Step 1', fields: ['subject'] },
72+
{ name: 's2', label: 'Step 2', fields: ['owner'] },
73+
{ name: 's3', label: 'Step 3', fields: ['notes'] },
74+
],
75+
} as any}
76+
dataSource={dataSource}
77+
/>,
78+
);
79+
return dataSource;
80+
}
81+
82+
const fill = (name: string, value: string) => {
83+
const input = document.body.querySelector<HTMLInputElement>(`[data-field="${name}"] input`);
84+
if (!input) throw new Error(`field not rendered: ${name}`);
85+
fireEvent.change(input, { target: { value } });
86+
};
87+
88+
const stepIndicator = (index: number) =>
89+
document.body.querySelectorAll<HTMLButtonElement>('nav[aria-label="Progress"] button')[index];
90+
91+
describe('#38276 — the submit-time gate does not judge the server lock on a CREATE wizard', () => {
92+
it('a CREATE wizard whose field `readonlyWhen` is TRUE over its values still demands the empty required field', async () => {
93+
const ds = mount('create', LOCK_TRUE_ON_CREATE);
94+
await waitFor(() => expect(document.body.querySelector('[data-field="subject"]')).toBeTruthy());
95+
fill('subject', 'S1');
96+
fireEvent.click(stepIndicator(2));
97+
await waitFor(() => expect(document.body.querySelector('[data-field="notes"]')).toBeTruthy());
98+
fill('notes', 'n');
99+
expect(document.body.querySelector('[data-field="owner"]')).toBeNull();
100+
fireEvent.click(screen.getByRole('button', { name: /Create/i }));
101+
// Refused: the wizard lands back on the step holding `owner`, nothing written.
102+
await waitFor(() => expect(document.body.querySelector('[data-field="owner"]')).toBeTruthy());
103+
expect(ds.create).not.toHaveBeenCalled();
104+
});
105+
106+
it('CONTROL — an EDIT wizard on a record where `readonlyWhen` holds treats the field as locked and saves without it', async () => {
107+
const ds = mount('edit', CONFIRMED_LOCK, { subject: 'Order', status: 'confirmed', owner: null, notes: '' });
108+
await waitFor(() => expect(ds.findOne).toHaveBeenCalled());
109+
await waitFor(() => expect(document.body.querySelector('[data-field="subject"]')).toBeTruthy());
110+
await waitFor(() => expect(document.body.querySelector<HTMLInputElement>('[data-field="subject"] input')?.value).toBe('Order'));
111+
fireEvent.click(stepIndicator(2));
112+
await waitFor(() => expect(document.body.querySelector('[data-field="notes"]')).toBeTruthy());
113+
fill('notes', 'followed up');
114+
fireEvent.click(screen.getByRole('button', { name: /Update/i }));
115+
await waitFor(() => expect(ds.update).toHaveBeenCalledTimes(1));
116+
});
117+
});

‎ui/packages/plugin-form/src/WizardForm.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,14 @@ export const WizardForm: React.FC<WizardFormProps> = ({
901901
const state = resolveFieldRuleState(
902902
{
903903
visibleWhen,
904-
readonlyWhen: (field as any).readonlyWhen,
904+
// The server lock is not judged on a create either (ADR-0163,
905+
// "The server lock": "A conditional lock is not judged on a
906+
// create, which is what makes `readonlyWhen: P\`true\``
907+
// set-once"): dropped on a CREATE wizard, as `form.tsx` drops it.
908+
// Judged here, a lock TRUE over the new record's values would
909+
// skip the field's required re-check and hand the empty field to
910+
// the server's insert refusal after the last step.
911+
readonlyWhen: isCreateWizard ? undefined : (field as any).readonlyWhen,
905912
// The hand lock is judged on an existing record only ("a create
906913
// is not judged", `@objectstack/spec` `Field.uiReadonlyWhen` and
907914
// `ObjectSchema.uiReadonlyWhen`): dropped on a CREATE wizard

0 commit comments

Comments
 (0)