Skip to content

Commit 0a32ccf

Browse files
authored
Use useForm validation mode: onSubmit instead of onTouched (#1787)
change shared useForm wrapper to mode: onSubmit
1 parent e74d5ae commit 0a32ccf

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

app/hooks/use-form.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
import { type FieldValues, type UseFormProps, useForm as _useForm } from 'react-hook-form'
99

1010
/**
11-
* Same as built-in `useForm` except `mode: 'onTouched'` is hard-coded and the
12-
* caller can't set it. `onTouched` means the first validation on a field is
13-
* triggered by blur, after which it updates with every change.
14-
*
11+
* Same as built-in `useForm` except we can hard-code some props and prevent the
12+
* caller from setting them.
1513
* See https://react-hook-form.com/docs/useform#mode
1614
*/
1715
export function useForm<TFieldValues extends FieldValues = FieldValues>(
1816
props?: Omit<UseFormProps<TFieldValues>, 'mode'>
1917
) {
20-
return _useForm({ mode: 'onTouched', ...props })
18+
return _useForm({ mode: 'onSubmit', ...props })
2119
}

app/test/e2e/instance-create.e2e.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,16 @@ test('can create an instance with custom hardware', async ({ page }) => {
107107

108108
// test disk size validation against image size
109109
await diskSizeInput.fill('5')
110-
await diskSizeInput.blur() // need blur to trigger validation
110+
111+
const submitButton = page.getByRole('button', { name: 'Create instance' })
112+
await submitButton.click() // submit to trigger validation
113+
111114
await expectVisible(page, [
112115
'main >> text=Must be as large as selected image (min. 6 GiB)',
113116
])
114117
await diskSizeInput.fill('10')
115118

116-
await page.getByRole('button', { name: 'Create instance' }).click()
119+
await submitButton.click()
117120

118121
await expect(page).toHaveURL(`/projects/mock-project/instances/${instanceName}/storage`)
119122

app/test/e2e/project-create.e2e.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ test.describe('Project create', () => {
3232
test('shows field-level validation error and does not POST', async ({ page }) => {
3333
await page.fill('role=textbox[name="Name"]', 'Invalid name')
3434

35-
await page.click('role=textbox[name="Description"]') // just to blur name input
36-
// role=dialog to distinguish from live announce
37-
await expectVisible(page, ['role=dialog >> text="Must start with a lower-case letter"'])
35+
// submit to trigger validation
36+
await page.getByRole('button', { name: 'Create project' }).click()
37+
38+
await expect(page.getByText('Must start with a lower-case letter').nth(0)).toBeVisible()
3839
})
3940

4041
test('shows form-level error for known server error', async ({ page }) => {

0 commit comments

Comments
 (0)