Skip to content

Commit c187b75

Browse files
authored
Consolidate number field min/max validation and revalidate when bounds change (#3376)
This is prep for upgrading React Aria, which will let us turn off clamping in the number input and make `NumberField` responsible for enforcing `min` and `max`. On main, `DiskSizeField` is the only number field that validates bounds for itself; the prefix length field on the external subnet form and every other `NumberField` with a `min` or `max` rely on the clamping. Once clamping is off, those fields need explicit validation. So, in this PR we: - **Move min/max validation into `NumberField` and delete `DiskSizeField`.** The validate rule that used to live in `DiskSizeField` now runs for every number field with a `min` or `max`, with the units appended to the message ("Can be at most 32", "Must be at least 10 GiB"). That left `DiskSizeField` as a wrapper setting `units="GiB"`, `required`, and `min={1}`. Its two callers now pass those directly. - **Add `deps` to `ListboxField`** and use it on the external subnet form so changing the pool re-validates prefix length. Without this, a stale "Can be at most 32" would hang around after switching back to a v6 pool, since by default react-hook-form only re-runs validation on the field that changed. - **Add `deps` to disk type radio in disk create**, so an existing size error updates (e.g., is cleared if appropriate) when switching between Local and Distributed instead of waiting for the next submit. This also fixes a bug on main: the clamp is display-only. Entering 64 on a v6 pool and switching to v4 shows 32 in the box, but form state still holds 64 and submit sends `prefix_length: 64`. With this change, submit is blocked with a message instead. The existing e2e specs for disks, external subnets, instance create, and subnet pools pass unchanged. The external subnet prefix length test gets a submit assertion covering the display-only clamp bug.
1 parent add81de commit c187b75

7 files changed

Lines changed: 47 additions & 68 deletions

File tree

‎app/components/form/fields/DiskSizeField.tsx‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

‎app/components/form/fields/ListboxField.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type Control,
1212
type FieldPath,
1313
type FieldValues,
14+
type RegisterOptions,
1415
} from 'react-hook-form'
1516

1617
import { Listbox, type ListboxItem } from '~/ui/lib/Listbox'
@@ -37,6 +38,8 @@ export type ListboxFieldProps<
3738
isLoading?: boolean
3839
noItemsPlaceholder?: string
3940
hideOptionalTag?: boolean
41+
/** Other fields to re-validate when this one changes */
42+
deps?: RegisterOptions<TFieldValues, TName>['deps']
4043
}
4144

4245
export function ListboxField<
@@ -57,10 +60,11 @@ export function ListboxField<
5760
noItemsPlaceholder,
5861
hideOptionalTag,
5962
hideLabel,
63+
deps,
6064
}: ListboxFieldProps<TFieldValues, TName>) {
6165
// TODO: recreate this logic
6266
// validate: (v) => (required && !v ? `${name} is required` : undefined),
63-
const { field, fieldState } = useController({ name, control, rules: { required } })
67+
const { field, fieldState } = useController({ name, control, rules: { required, deps } })
6468
return (
6569
<div className={cn('max-w-lg', className)}>
6670
<Listbox

‎app/components/form/fields/NumberField.tsx‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ export function NumberField<
4444
)}
4545
</div>
4646
{/* passing the generated id is very important for a11y */}
47-
<NumberFieldInner name={name} id={id} label={label} required={required} {...props} />
47+
<NumberFieldInner
48+
name={name}
49+
id={id}
50+
label={label}
51+
units={units}
52+
required={required}
53+
{...props}
54+
/>
4855
</div>
4956
)
5057
}
@@ -72,6 +79,7 @@ export const NumberFieldInner = <
7279
disabled,
7380
max,
7481
min = 0,
82+
units,
7583
}: TextFieldProps<TFieldValues, TName>) => {
7684
const generatedId = useId()
7785
const id = idProp || generatedId
@@ -85,9 +93,16 @@ export const NumberFieldInner = <
8593
rules: {
8694
required,
8795
deps,
88-
// it seems we need special logic to enforce required on NaN
96+
// RHF's required rule doesn't catch NaN, and its min/max rules don't
97+
// know about units, so we do all three here. The input itself no longer
98+
// clamps, so this is what stops out-of-range values.
8999
validate(value, values) {
90-
if (required && Number.isNaN(value)) return `${label} is required`
100+
if (Number.isNaN(value)) return required ? `${label} is required` : undefined
101+
const suffix = units ? ` ${units}` : ''
102+
if (min !== undefined && value < Number(min))
103+
return `Must be at least ${min}${suffix}`
104+
if (max !== undefined && value > Number(max))
105+
return `Can be at most ${max}${suffix}`
91106
return validate?.(value, values)
92107
},
93108
},

‎app/forms/disk-create.tsx‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import {
2424

2525
import { CheckboxField } from '~/components/form/fields/CheckboxField'
2626
import { DescriptionField } from '~/components/form/fields/DescriptionField'
27-
import { DiskSizeField } from '~/components/form/fields/DiskSizeField'
2827
import { toImageComboboxItem } from '~/components/form/fields/ImageSelectField'
2928
import { ListboxField } from '~/components/form/fields/ListboxField'
3029
import { NameField } from '~/components/form/fields/NameField'
30+
import { NumberField } from '~/components/form/fields/NumberField'
3131
import { RadioField } from '~/components/form/fields/RadioField'
3232
import { SideModalForm } from '~/components/form/SideModalForm'
3333
import { HL } from '~/components/HL'
@@ -207,9 +207,12 @@ export function CreateDiskSideModalForm({
207207
}}
208208
/>
209209
<DescriptionField name="description" control={form.control} />
210-
<DiskSizeField
210+
<NumberField
211211
name="size"
212212
control={form.control}
213+
units="GiB"
214+
required
215+
min={1}
213216
// Local disk size is only capped by server capacity
214217
max={match(diskBackend)
215218
.with({ type: 'local' }, () => undefined)
@@ -242,7 +245,12 @@ const DiskBackendField = ({
242245
}) => {
243246
const {
244247
field: { value: diskBackend, onChange },
245-
} = useController({ control, name: 'diskBackend' })
248+
} = useController({
249+
control,
250+
name: 'diskBackend',
251+
// Switching disk type changes the size limit.
252+
rules: { deps: 'size' },
253+
})
246254
// react-hook-form types onChange as (...event: any[]) => void
247255
// https://github.com/react-hook-form/react-hook-form/issues/10466
248256
const setDiskBackend: (value: DiskBackendForm) => void = onChange

‎app/forms/external-subnet-create.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export default function CreateExternalSubnetSideModalForm() {
132132
items={pools.items.map(toPoolItem)}
133133
required
134134
description="Subnet pool to allocate from"
135+
// the pool's IP version sets the prefix length max
136+
deps="prefixLength"
135137
/>
136138
<NumberField
137139
name="prefixLength"

‎app/forms/instance-create.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import { DocsPopover } from '~/components/DocsPopover'
4949
import { CheckboxField } from '~/components/form/fields/CheckboxField'
5050
import { ComboboxField } from '~/components/form/fields/ComboboxField'
5151
import { DescriptionField } from '~/components/form/fields/DescriptionField'
52-
import { DiskSizeField } from '~/components/form/fields/DiskSizeField'
5352
import {
5453
DisksTableField,
5554
type DiskTableItem,
@@ -487,11 +486,13 @@ export default function CreateInstanceForm() {
487486
const bootDiskSizeAndName = (
488487
<>
489488
<div key="divider1" className="my-6! content-['a']" />
490-
<DiskSizeField
489+
<NumberField
491490
key="diskSizeField"
492491
label="Disk size"
493492
name="bootDiskSize"
494493
control={control}
494+
units="GiB"
495+
required
495496
min={imageSizeGiB || 1}
496497
// Max size applies: this disk can only be distributed
497498
max={MAX_DISK_SIZE_GiB}

‎test/e2e/external-subnets.e2e.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ test('create form prefix length max changes with pool IP version', async ({ page
393393
// Switch back to v4 — value should clamp back to 32
394394
await selectOption(page, 'Subnet pool', v4Pool)
395395
await expect(prefixLen).toHaveValue('32')
396+
397+
// The clamp is display-only: the form still holds 64, so submit must be
398+
// blocked with a message rather than sending an out-of-range prefix
399+
await page.getByRole('textbox', { name: 'Name' }).fill('too-long')
400+
await page.getByRole('button', { name: 'Create external subnet' }).click()
401+
const dialog = page.getByRole('dialog', { name: 'Create external subnet' })
402+
await expect(dialog.getByText('Can be at most 32')).toBeVisible()
403+
await expect(dialog).toBeVisible()
396404
})
397405

398406
test('create form toggles between auto and explicit fields', async ({ page }) => {

0 commit comments

Comments
 (0)