Skip to content

Commit f495162

Browse files
committed
Added array proxy example
1 parent 2cc77f9 commit f495162

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { zod } from '$lib/adapters/zod.js';
2+
import { message, superValidate } from '$lib/server/index.js';
3+
import { schema } from './schema.js';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate(zod(schema));
8+
return { form };
9+
};
10+
11+
export const actions = {
12+
default: async ({ request }) => {
13+
const formData = await request.formData();
14+
console.log(formData);
15+
16+
const form = await superValidate(formData, zod(schema));
17+
console.log(form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Posted OK!');
22+
}
23+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="ts">
2+
import ArrayField from './ArrayField.svelte';
3+
import { superForm } from '$lib/index.js';
4+
import { zod } from '$lib/adapters/zod.js';
5+
import { schema } from './schema.js';
6+
7+
export let data;
8+
9+
const form = superForm(data.form, {
10+
validators: zod(schema)
11+
});
12+
</script>
13+
14+
<ArrayField {form} field="tags" newValue="new" />
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<script lang="ts" context="module">
2+
type T = Record<string, unknown>;
3+
type F = FormPathArrays<T>;
4+
</script>
5+
6+
<script lang="ts" generics="T extends Record<string, unknown>, F extends FormPathArrays<T>">
7+
import type { Writable } from 'svelte/store';
8+
import { arrayProxy } from '$lib/index.js';
9+
import type { SuperForm, FormPathArrays, FormPathType } from '$lib/index.js';
10+
11+
export let form: SuperForm<T>;
12+
export let field: F;
13+
export let newValue: FormPathType<T, F> extends (infer S)[] ? S : never;
14+
15+
const { values: v, errors, valueErrors } = arrayProxy(form, field);
16+
const values = v as Writable<(typeof newValue)[]>;
17+
</script>
18+
19+
<div>
20+
<ol>
21+
<!-- eslint-disable-next-line @typescript-eslint/no-unused-vars -->
22+
{#each $values as _, i}
23+
<li>
24+
<button type="button" title="Delete" on:click={() => ($values = $values.toSpliced(i, 1))}>
25+
✖️
26+
</button>
27+
{#if $valueErrors && $valueErrors[i]}
28+
<div role="alert">{$valueErrors[i]}</div>
29+
{/if}
30+
</li>
31+
{/each}
32+
<button type="button" on:click={() => ($values = [...$values, newValue])}> Add </button>
33+
</ol>
34+
{#if $errors}
35+
<div role="alert">{$errors}</div>
36+
{/if}
37+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
tags: z.array(z.string().min(1)).nonempty()
5+
});

0 commit comments

Comments
 (0)