Skip to content

Commit 2fbc796

Browse files
committed
Test page for valid store.
1 parent a18e68d commit 2fbc796

File tree

3 files changed

+159
-4
lines changed

3 files changed

+159
-4
lines changed

src/lib/client/validateField.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ export async function validateField<T extends AnyZodObject, M>(
124124
return get(Errors);
125125
}
126126

127-
function Errors_exist() {
128-
return errorsExist(get(Errors));
129-
}
130-
131127
function Errors_clear(
132128
options: NonNullable<Parameters<typeof clearErrors>[1]>
133129
) {

src/routes/valid/+page.server.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { message, superValidate } from '$lib/server';
2+
import { error, redirect } from '@sveltejs/kit';
3+
import { users, userId, userSchema } from '../users';
4+
5+
const schema = userSchema.extend({
6+
id: userSchema.shape.id.optional()
7+
});
8+
9+
///// Load //////////////////////////////////////////////////////////
10+
11+
export const load = async ({ url }) => {
12+
// READ user
13+
// For simplicity, use the id query parameter instead of a route.
14+
const id = url.searchParams.get('id');
15+
const user = id ? users.find((u) => u.id == id) : null;
16+
17+
if (id && !user) throw error(404, 'User not found.');
18+
19+
const form = await superValidate(user, schema);
20+
return { form, users };
21+
};
22+
23+
///// Form actions //////////////////////////////////////////////////
24+
25+
export const actions = {
26+
default: async (event) => {
27+
const data = await event.request.formData();
28+
29+
const form = await superValidate(data, schema);
30+
if (!form.valid) return message(form, 'Invalid form');
31+
32+
if (!form.data.id) {
33+
// CREATE user
34+
const user = { ...form.data, id: userId() };
35+
users.push(user);
36+
37+
return message(form, 'User created!');
38+
} else {
39+
const user = users.find((u) => u.id == form.data.id);
40+
if (!user) throw error(404, 'User not found.');
41+
42+
const index = users.indexOf(user);
43+
44+
if (data.has('delete')) {
45+
// DELETE user
46+
users.splice(index, 1);
47+
throw redirect(303, '?');
48+
} else {
49+
// UPDATE user
50+
users[index] = { ...form.data, id: user.id };
51+
return message(form, 'User updated!');
52+
}
53+
}
54+
}
55+
};

src/routes/valid/+page.svelte

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<script lang="ts">
2+
import type { PageData } from './$types';
3+
import { page } from '$app/stores';
4+
import { superForm } from '$lib/client';
5+
import { userSchema } from '../users';
6+
7+
export let data: PageData;
8+
9+
const { form, errors, enhance, delayed, message, valid } = superForm(
10+
data.form,
11+
{
12+
validators: $page.url.searchParams.has('zod')
13+
? userSchema.extend({
14+
id: userSchema.shape.id.optional()
15+
})
16+
: {
17+
name: (name: string) =>
18+
name.length < 2 ? 'Your name is too short' : null,
19+
email: (email: string) =>
20+
!email.includes('@') ? 'Enter a valid email address' : null
21+
}
22+
}
23+
);
24+
</script>
25+
26+
<a href="/">&lt; Back to start</a>
27+
28+
<h1>sveltekit-superforms</h1>
29+
30+
<div class="users">
31+
{#each data.users as user}
32+
<a href="?id={user.id}">{user.name}</a>
33+
{/each}
34+
{#if $form.id}
35+
<form action="/crud">
36+
<button>Create new</button>
37+
</form>
38+
{/if}
39+
</div>
40+
41+
{#if $message}
42+
<h3 class:invalid={$page.status >= 400}>{$message}</h3>
43+
{/if}
44+
45+
<h2>{!$form.id ? 'Create' : 'Update'} user</h2>
46+
47+
<b>Valid: {$valid}</b>
48+
49+
<form method="POST" use:enhance>
50+
<input type="hidden" name="id" bind:value={$form.id} />
51+
52+
<label>
53+
Name<br />
54+
<input name="name" data-invalid={$errors.name} bind:value={$form.name} />
55+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
56+
</label>
57+
58+
<label>
59+
E-mail<br />
60+
<input
61+
name="email"
62+
type="email"
63+
data-invalid={$errors.email}
64+
bind:value={$form.email}
65+
/>
66+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
67+
</label>
68+
69+
<button>Submit</button>
70+
{#if $delayed}Working...{/if}
71+
72+
{#if $form.id}
73+
<button
74+
name="delete"
75+
value="delete"
76+
on:click={(e) => !confirm('Are you sure?') && e.preventDefault()}
77+
class="danger">Delete user</button
78+
>
79+
{/if}
80+
</form>
81+
82+
<style>
83+
.invalid {
84+
color: red;
85+
}
86+
87+
.danger {
88+
background-color: brown;
89+
}
90+
91+
.users {
92+
columns: 3 150px;
93+
}
94+
95+
.users > * {
96+
display: block;
97+
white-space: nowrap;
98+
overflow-x: hidden;
99+
}
100+
101+
.users a:hover {
102+
border-bottom: none;
103+
}
104+
</style>

0 commit comments

Comments
 (0)