Skip to content

Commit dfc01a4

Browse files
committed
Added test for possible index problem in errors
1 parent f8eb757 commit dfc01a4

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

src/routes/(v2)/v2/Navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
'validate-update',
3535
'validators-clear',
3636
'submit-prog',
37-
'modify-reset'
37+
'modify-reset',
38+
'index-errors'
3839
].sort();
3940
</script>
4041

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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
//import { zod } from '$lib/adapters/zod.js'
5+
//import { schema } from './schema.js';
6+
7+
export let data;
8+
9+
const { form, errors, tainted, message, enhance } = superForm(data.form, {
10+
taintedMessage: false
11+
//dataType: 'json',
12+
//validators: zod(schema)
13+
});
14+
</script>
15+
16+
<SuperDebug data={{ $form, $errors, $tainted }} />
17+
18+
{#if $message}<h4>{$message}</h4>{/if}
19+
20+
<form method="POST" use:enhance>
21+
{#if $form && $form.instances && $form.instances.length > 0}
22+
{#each $form.instances as item, i}
23+
<label
24+
class="CrispLabel"
25+
data-align="center"
26+
data-direction="row"
27+
for={`instances[${i}].name`}
28+
style="justify-content: space-between;"
29+
>
30+
<span data-mandatory style="color: inherit;"> Name </span>
31+
<input
32+
type="text"
33+
class="CrispInput"
34+
bind:value={item.name}
35+
name={`instances[${i}].name`}
36+
style="--crp-input-width: 70%;"
37+
/>
38+
</label>
39+
{#if $errors.instances && $errors.instances[i] && $errors.instances[i].name}
40+
<ul class="CrispMessageList w-100">
41+
{#each $errors.instances[i].name ?? [] as error}
42+
<li class="CrispMessage" data-type="error">{error}</li>
43+
{/each}
44+
</ul>
45+
{/if}
46+
{/each}
47+
{/if}
48+
<div>
49+
<button>Submit</button>
50+
</div>
51+
</form>
52+
53+
<style lang="scss">
54+
form {
55+
margin: 2rem 0;
56+
57+
input {
58+
background-color: #dedede;
59+
}
60+
61+
.invalid {
62+
color: crimson;
63+
}
64+
}
65+
</style>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { z } from 'zod';
2+
3+
const itemSchema = z.object({
4+
name: z.string()
5+
});
6+
7+
export const schema = z.object({
8+
instances: z.array(itemSchema)
9+
});

0 commit comments

Comments
 (0)