Skip to content

Commit 6787390

Browse files
committed
Added form validation to add reference link
1 parent 45e131d commit 6787390

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed
Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,60 @@
11
<script lang="ts">
2-
import { Button } from '$comp/ui/button';
32
import * as Dialog from '$comp/ui/dialog';
3+
import * as Form from '$comp/ui/form';
44
import { Input } from '$comp/ui/input';
5-
import { Label } from '$comp/ui/label';
5+
import { defaults, superForm } from 'sveltekit-superforms';
6+
import { classvalidatorClient } from 'sveltekit-superforms/adapters';
7+
8+
import { ReferenceLinkForm } from '../models';
69
710
interface Props {
811
open: boolean;
912
save: (url: string) => Promise<void>;
1013
}
1114
12-
// TODO: Use form validation
1315
let { open = $bindable(), save }: Props = $props();
14-
let value = $state('');
1516
16-
async function onSubmit() {
17-
await save(value);
18-
open = false;
19-
}
17+
const form = superForm(defaults(new ReferenceLinkForm(), classvalidatorClient(ReferenceLinkForm)), {
18+
dataType: 'json',
19+
async onUpdate({ form }) {
20+
if (!form.valid) {
21+
return;
22+
}
23+
24+
await save(form.data.url);
25+
open = false;
26+
},
27+
SPA: true,
28+
validators: classvalidatorClient(ReferenceLinkForm)
29+
});
30+
31+
const { enhance, form: formData } = form;
2032
</script>
2133

2234
<Dialog.Root bind:open>
2335
<Dialog.Content class="sm:max-w-[425px]">
24-
<Dialog.Header>
25-
<Dialog.Title>Add Reference Link</Dialog.Title>
26-
<Dialog.Description>Add a reference link to an external resource.</Dialog.Description>
27-
</Dialog.Header>
28-
<div class="grid gap-4 py-4">
29-
<div class="grid grid-cols-5 items-center gap-6">
30-
<Label for="url" class="text-right">Url</Label>
31-
<Input id="url" name="url" type="url" bind:value placeholder="Please enter a valid URL" class="col-span-4" required />
36+
<form method="POST" use:enhance>
37+
<Dialog.Header>
38+
<Dialog.Title>Add Reference Link</Dialog.Title>
39+
<Dialog.Description>Add a reference link to an external resource.</Dialog.Description>
40+
</Dialog.Header>
41+
42+
<div class="py-4">
43+
<Form.Field {form} name="url">
44+
<Form.Control>
45+
{#snippet children({ props })}
46+
<Form.Label>Reference Link</Form.Label>
47+
<Input {...props} bind:value={$formData.url} type="url" placeholder="Please enter a valid URL" autocomplete="url" required />
48+
{/snippet}
49+
</Form.Control>
50+
<Form.Description />
51+
<Form.FieldErrors />
52+
</Form.Field>
3253
</div>
33-
</div>
34-
<Dialog.Footer>
35-
<Button type="submit" onclick={onSubmit}>Save Reference Link</Button>
36-
</Dialog.Footer>
54+
55+
<Dialog.Footer>
56+
<Form.Button>Save Reference Link</Form.Button>
57+
</Dialog.Footer>
58+
</form>
3759
</Dialog.Content>
3860
</Dialog.Root>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
export { Stack, StackStatus } from '$generated/api';
2+
import { IsUrl } from 'class-validator';
3+
4+
export class ReferenceLinkForm {
5+
@IsUrl({ require_tld: false })
6+
url!: string;
7+
}

0 commit comments

Comments
 (0)