cheogram-www
created pr with
cheogram-www/validate-form-on-execute.1
added cheogram-www/validate-form-on-execute.2
1: a03bb2b = 1: a03bb2b Use reportValidity to trigger browser validations
added cheogram-www/validate-form-on-execute.3
1: a03bb2b < -: ------- Use reportValidity to trigger browser validations
-: ------- > 1: c241180 Use reportValidity to trigger browser validations
cmds
checkout latest patchset:
ssh pr.pico.sh pull cheogram-www:validate-form-on-execute | git am -3checkout specific patchset revision:
ssh pr.pico.sh pull cheogram-www:validate-form-on-execute [rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh cheogram-www:validate-form-on-executeadd comment to patch request:
ssh pr.pico.sh comment cheogram-www:validate-form-on-execute "lgtm!"
Patchset
cheogram-www/validate-form-on-execute.3
Use reportValidity to trigger browser validations
Eric Roberts
2026-11-20T17:04:41ZExcluded cancel and prev as you wouldn't want to have to fill in something just to cancel or go back. I'm assuming all other actions should be validated, but this may turn out to be a bad assumption, I'm not sure. Normally clicking a button of type submit would do this validation, and we add type submit to buttons for the first action. However because the execute then causes the button to not exist on the page, the event never makes it to the form. I tried moving execute to the form onSubmit, and then making the buttons be submit, so it would just trigger the default validation behaviour. But this had other problems like in ButtonGrid closing the dialog for custom input causes the button not to be rendered and the form submit not to happen. So this seems like the smallest change that is reasonable to make with what I know right now. I still don't love it.
Diff stat
2 files changed, 113 insertions(+)
Semantic diff summary
0 added,
0 modified,
0 signature changed,
0 removed
across 2 analyzed files
+106
-0
src/components/CommandSession.test.tsx
#
| ... | ... | @@ -0,0 +1,106 @@ | |
| 1 | + | // @vitest-environment jsdom | |
| 2 | + | ||
| 3 | + | import type * as borogove from "borogove"; | |
| 4 | + | import { cleanup, fireEvent, render, screen } from "@testing-library/react"; | |
| 5 | + | import { afterEach, expect, it, vi } from "vitest"; | |
| 6 | + | import { mock } from "vitest-mock-extended"; | |
| 7 | + | import { CommandSession } from "./CommandSession"; | |
| 8 | + | ||
| 9 | + | afterEach(() => { | |
| 10 | + | cleanup(); | |
| 11 | + | }); | |
| 12 | + | ||
| 13 | + | const setup = async () => { | |
| 14 | + | const session = mock<borogove.CommandSession>({ | |
| 15 | + | status: "executing", | |
| 16 | + | forms: [ | |
| 17 | + | mock<borogove.Form>({ | |
| 18 | + | isResult: () => false, | |
| 19 | + | items: () => [ | |
| 20 | + | mock<borogove.FormItem>({ | |
| 21 | + | section: null, | |
| 22 | + | tableHeader: null, | |
| 23 | + | field: mock<borogove.FormField>({ | |
| 24 | + | name: "name", | |
| 25 | + | label: "Name", | |
| 26 | + | type: "text-single", | |
| 27 | + | datatype: "xs:string", | |
| 28 | + | required: true, | |
| 29 | + | value: [], | |
| 30 | + | options: [], | |
| 31 | + | }), | |
| 32 | + | }), | |
| 33 | + | ], | |
| 34 | + | }), | |
| 35 | + | ], | |
| 36 | + | actions: ["next", "complete", "execute", "custom", "cancel", "prev"].map( | |
| 37 | + | (value) => ({ label: value, value, svg: null }), | |
| 38 | + | ), | |
| 39 | + | }); | |
| 40 | + | ||
| 41 | + | render( | |
| 42 | + | <CommandSession | |
| 43 | + | session={Promise.resolve(session)} | |
| 44 | + | idx={0} | |
| 45 | + | updateSession={vi.fn()} | |
| 46 | + | />, | |
| 47 | + | ); | |
| 48 | + | ||
| 49 | + | const input = await screen.findByLabelText(/^Name/); | |
| 50 | + | ||
| 51 | + | // Without this we get an error when the button is clicked because it tries | |
| 52 | + | // to submit the form and jsdom doesn't implement native submit. | |
| 53 | + | // | |
| 54 | + | // In real life updating the session causes the button not to be rendered | |
| 55 | + | // and the form doesn't get submitted anyway. | |
| 56 | + | input | |
| 57 | + | .closest("form")! | |
| 58 | + | .addEventListener("submit", (event) => event.preventDefault()); | |
| 59 | + | ||
| 60 | + | return session; | |
| 61 | + | }; | |
| 62 | + | ||
| 63 | + | it.each(["next", "complete", "execute", "custom"])( | |
| 64 | + | "blocks %s on an invalid form", | |
| 65 | + | async (action) => { | |
| 66 | + | const session = await setup(); | |
| 67 | + | ||
| 68 | + | fireEvent.change(screen.getByLabelText(/^Name/), { | |
| 69 | + | target: { value: "" }, | |
| 70 | + | }); | |
| 71 | + | fireEvent.click(screen.getByRole("button", { name: action })); | |
| 72 | + | ||
| 73 | + | expect(session.execute).not.toHaveBeenCalled(); | |
| 74 | + | }, | |
| 75 | + | ); | |
| 76 | + | ||
| 77 | + | it.each(["next", "complete", "execute", "custom"])( | |
| 78 | + | "submits %s on a valid form", | |
| 79 | + | async (action) => { | |
| 80 | + | const session = await setup(); | |
| 81 | + | ||
| 82 | + | fireEvent.change(screen.getByLabelText(/^Name/), { | |
| 83 | + | target: { value: "Alice" }, | |
| 84 | + | }); | |
| 85 | + | fireEvent.click(screen.getByRole("button", { name: action })); | |
| 86 | + | ||
| 87 | + | expect(session.execute).toHaveBeenCalledWith(action, expect.any(FormData)); | |
| 88 | + | expect((session.execute.mock.calls[0][1] as FormData).get("name")).toBe( | |
| 89 | + | "Alice", | |
| 90 | + | ); | |
| 91 | + | }, | |
| 92 | + | ); | |
| 93 | + | ||
| 94 | + | it.each(["cancel", "prev"])( | |
| 95 | + | "allows %s with an incomplete form", | |
| 96 | + | async (action) => { | |
| 97 | + | const session = await setup(); | |
| 98 | + | ||
| 99 | + | fireEvent.change(screen.getByLabelText(/^Name/), { | |
| 100 | + | target: { value: "" }, | |
| 101 | + | }); | |
| 102 | + | fireEvent.click(screen.getByRole("button", { name: action })); | |
| 103 | + | ||
| 104 | + | expect(session.execute).toHaveBeenCalledWith(action, expect.any(FormData)); | |
| 105 | + | }, | |
| 106 | + | ); |
+7
-0
src/components/CommandSession.tsx
#
| ... | ... | @@ -34,6 +34,13 @@ const CommandSessionInner = ({ | |
| 34 | 34 | } | |
| 35 | 35 | ||
| 36 | 36 | const execute = (action: string | null) => { | |
| 37 | + | if ( | |
| 38 | + | !["cancel", "prev"].includes(action ?? "") && | |
| 39 | + | formRef.current?.reportValidity() === false | |
| 40 | + | ) { | |
| 41 | + | return; | |
| 42 | + | } | |
| 43 | + | ||
| 37 | 44 | const data = | |
| 38 | 45 | formRef.current && !form.isResult() | |
| 39 | 46 | ? new FormData(formRef.current) |