Skip to content

Commit d0c0cc4

Browse files
authored
Revalidate disk size when source changes (#3395)
Like #3394, pulled out of my work on #3391. Without this, if you submit the disk create form with a size too small for the selected source, and then switch to a smaller source that fits, the "Must be as large as selected…" error stays up until you edit the size again. Adding `deps="size"` to both source pickers revalidates the size whenever the source changes. `deps` is confusingly named because it makes it sounds like source depends on size, but the [docs](https://react-hook-form.com/docs/useform/register) for deps say: > When this field's own validation runs (e.g. via its registered onChange/onBlur), the fields listed in deps will also be re-validated.
1 parent 4f1ba37 commit d0c0cc4

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎app/forms/disk-create.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ const DiskSourceField = ({
373373
isLoading={areImagesLoading}
374374
items={images.map((i) => toImageComboboxItem(i, true))}
375375
required
376+
// revalidate size against the new source's minimum
377+
deps="size"
376378
onChange={(id) => {
377379
const image = images.find((i) => i.id === id)!
378380
const imageSizeGiB = image.size / GiB
@@ -444,6 +446,8 @@ const SnapshotSelectField = ({ control }: { control: Control<DiskCreateForm> })
444446
})}
445447
isLoading={snapshotsQuery.isPending}
446448
required
449+
// revalidate size against the new source's minimum
450+
deps="size"
447451
onChange={(id) => {
448452
const snapshot = snapshots.find((i) => i.id === id)! // if it's selected, it must be present
449453
const snapshotSizeGiB = snapshot.size / GiB

‎test/e2e/disks.e2e.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,42 @@ test('Create local disk with size > 1023 GiB', async ({ page }) => {
299299
})
300300
})
301301

302+
test('Size error clears when switching to a smaller source', async ({ page }) => {
303+
await page.goto('/projects/mock-project/disks-new')
304+
const dialog = page.getByRole('dialog', { name: 'Create disk' })
305+
await page.getByRole('textbox', { name: 'Name' }).fill('a-new-disk')
306+
const sizeInput = page.getByRole('textbox', { name: 'Size (GiB)' })
307+
const createButton = page.getByRole('button', { name: 'Create disk' })
308+
309+
await page.getByRole('radio', { name: 'Snapshot' }).click()
310+
await page.getByRole('button', { name: 'Source snapshot' }).click()
311+
await page.getByRole('option', { name: 'snapshot-heavy' }).click()
312+
await fillNumberInput(sizeInput, '5')
313+
await createButton.click()
314+
const snapshotError = dialog.getByText(
315+
'Must be as large as selected snapshot (min. 20 GiB)'
316+
)
317+
await expect(snapshotError).toBeVisible()
318+
319+
// 5 GiB is plenty for the new snapshot, so the error should go away
320+
await page.getByRole('button', { name: 'Source snapshot' }).click()
321+
await page.getByRole('option', { name: /^snapshot-1 / }).click()
322+
await expect(snapshotError).toBeHidden()
323+
324+
await page.getByRole('radio', { name: 'Image' }).click()
325+
await page.getByRole('button', { name: 'Source image' }).click()
326+
await page.getByRole('option', { name: 'image-4' }).click()
327+
await fillNumberInput(sizeInput, '5')
328+
await createButton.click()
329+
const imageError = dialog.getByText('Must be as large as selected image (min. 7 GiB)')
330+
await expect(imageError).toBeVisible()
331+
332+
// 5 GiB is plenty for the 4 GiB image
333+
await page.getByRole('button', { name: 'Source image' }).click()
334+
await page.getByRole('option', { name: 'image-1' }).click()
335+
await expect(imageError).toBeHidden()
336+
})
337+
302338
test('Create disk from snapshot with read-only', async ({ page }) => {
303339
await page.goto('/projects/mock-project/disks-new')
304340
await page.getByRole('textbox', { name: 'Name' }).fill('a-new-disk')

0 commit comments

Comments
 (0)