Skip to content

Commit bef01a5

Browse files
committed
Added failing Formsnap test.
1 parent e2c4a64 commit bef01a5

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
import Field from './Field.svelte';
4+
5+
export let data;
6+
7+
const form = superForm(data.form, {
8+
taintedMessage: false
9+
});
10+
</script>
11+
12+
<form method="POST" use:form.enhance>
13+
<Field {form} name="flavors" />
14+
<div>
15+
<button>Submit</button>
16+
</div>
17+
</form>
18+
19+
<style lang="scss">
20+
form {
21+
margin: 2rem 0;
22+
23+
input {
24+
background-color: #dedede;
25+
}
26+
27+
.invalid {
28+
color: crimson;
29+
}
30+
}
31+
</style>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script lang="ts" context="module">
2+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3+
import type { FormPath } from '$lib/index.js';
4+
type T = Record<string, unknown>;
5+
type U = unknown;
6+
</script>
7+
8+
<script lang="ts" generics="T extends Record<string, unknown>, U extends FormPath<T>">
9+
import type { SuperForm } from 'sveltekit-superforms';
10+
export let form: SuperForm<T>;
11+
export let name: U;
12+
</script>
13+
14+
<!--
15+
@component
16+
## Field
17+
A component that provides the necessary context for a form field.
18+
19+
- [Field Documentation](https://formsnap.dev/docs/components/field)
20+
21+
### Slot Props
22+
- `value` - The value of the field.
23+
- `errors` - The errors of the field.
24+
- `tainted` - The tainted state of the field.
25+
- `constraints` - The constraints of the field.
26+
27+
@param {SuperForm} form - The form object.
28+
@param {FormPath<T>} name - The name of the field.
29+
-->
30+
31+
<slot />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { z } from 'zod';
2+
3+
/*
4+
type T = {
5+
flavors: ('vanilla' | 'chocolate' | 'cookies and cream' | 'strawberry')[];
6+
toppings: ('sprinkles' | 'hot fudge' | 'whipped cream' | 'cherry')[];
7+
};
8+
*/
9+
10+
export const schema = z.object({
11+
flavors: z.enum(['vanilla', 'chocolate', 'cookies and cream', 'strawberry']).array(),
12+
toppings: z.enum(['sprinkles', 'hot fudge', 'whipped cream', 'cherry']).array()
13+
});

0 commit comments

Comments
 (0)