This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
/
+page.svelte
83 lines (69 loc) · 1.65 KB
/
+page.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<script lang="ts">
import type { PageData } from './$types';
import { superForm } from 'sveltekit-superforms/client';
import type { Validation } from 'sveltekit-superforms';
import { userSchema } from '$lib/user';
export let data: PageData;
// For now, construct Validation object manually. This will be fixed
// in a future version of sveltekit-superforms.
const validation: Validation<typeof userSchema> = {
valid: true,
empty: false,
errors: {},
constraints: {},
data: data.user
};
const { form, errors, message, constraints, enhance, delayed } = superForm<typeof userSchema>(
validation,
{
validators: userSchema,
onError({ result, message }) {
message.set(result.error.message);
}
}
);
</script>
<h1>Edit user</h1>
{#if $message}<h3>{$message}</h3>{/if}
<form method="POST" use:enhance>
<input type="hidden" name="id" bind:value={$form.id} />
<label>
Name<br />
<input name="name" data-invalid={$errors.name} bind:value={$form.name} {...$constraints.name} />
{#if $errors.name}<div class="invalid">{$errors.name}</div>{/if}
</label>
<label>
E-mail<br />
<input
name="email"
type="email"
data-invalid={$errors.email}
bind:value={$form.email}
{...$constraints.email}
/>
{#if $errors.email}<div class="invalid">{$errors.email}</div>{/if}
</label>
<div class="mt">
<button>Update</button>
{#if $delayed}Working...{/if}
</div>
</form>
<hr class="mt" />
<p><a href="/">< Back to start</a></p>
<style>
.mt {
margin-top: 2rem;
}
.invalid {
color: red;
}
input {
width: auto;
max-width: 300px;
}
form {
display: flex;
flex-direction: column;
align-items: center;
}
</style>