Skip to content

Commit cf4602f

Browse files
committed
Fixes #230.
Ignoring normal form actions.
1 parent f8bbccf commit cf4602f

File tree

9 files changed

+226
-2
lines changed

9 files changed

+226
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Fixed exception message when the `dataType` option isn't set to `'json'` and the schema contains a nested object.
13+
- Superforms are now ignoring normal SvelteKit form actions when they are posted. ([#230](https://github.com/ciscoheat/sveltekit-superforms/issues/230))
1314

1415
## [1.2.0] - 2023-07-06
1516

src/lib/client/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,6 @@ export function superForm<
806806
if (pageUpdate.form.type == 'error') return;
807807

808808
const forms = Context_findValidationForms(pageUpdate.form);
809-
if (!forms.length) error('$page.form (ActionData)');
810-
811809
for (const newForm of forms) {
812810
//console.log('🚀~ ActionData ~ newForm:', newForm.id);
813811
if (newForm.id !== _formId) continue;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { superValidate } from '$lib/server';
2+
import { z } from 'zod';
3+
4+
const schema = z.object({
5+
name: z.string().default('Hello world!'),
6+
email: z.string().email()
7+
});
8+
9+
export const load = async () => {
10+
// Server API:
11+
const form = await superValidate(schema);
12+
13+
// Always return { form } in load and form actions.
14+
return { form };
15+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<script lang="ts">
2+
import type { PageData } from './$types';
3+
import { superForm } from '$lib/client';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
6+
export let data: PageData;
7+
8+
const { form, errors, enhance, message } = superForm(data.form, {
9+
onError: ({ result }) => {
10+
$message = result.error.message;
11+
}
12+
});
13+
</script>
14+
15+
<SuperDebug data={{ $form, $message }} />
16+
17+
{#if $message}
18+
<div class="invalid">Error message: {$message}</div>
19+
{/if}
20+
21+
<form method="POST" action="/tests/issue-227/endpoint" use:enhance>
22+
<label for="name">Name</label>
23+
<input
24+
type="text"
25+
name="name"
26+
aria-invalid={$errors.name ? 'true' : undefined}
27+
bind:value={$form.name}
28+
/>
29+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
30+
31+
<label for="email">E-mail</label>
32+
<input
33+
type="email"
34+
name="email"
35+
aria-invalid={$errors.email ? 'true' : undefined}
36+
bind:value={$form.email}
37+
/>
38+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
39+
40+
<div><button>Submit</button></div>
41+
</form>
42+
43+
<style>
44+
.invalid {
45+
color: red;
46+
padding: 1rem 0;
47+
font-weight: bold;
48+
}
49+
</style>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { RequestHandler } from './$types';
2+
3+
export const POST: RequestHandler = async () => {
4+
return new Response('Body limit response', { status: 413 });
5+
};
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { superForm, superValidateSync } from '$lib/client';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
import { z } from 'zod';
6+
7+
const schema = z.object({
8+
name: z.string().min(1),
9+
email: z.string().email()
10+
});
11+
12+
const { form, errors, message, enhance } = superForm(
13+
superValidateSync(schema)
14+
);
15+
</script>
16+
17+
<div style="border: 3px solid red; padding: 10px;">
18+
<SuperDebug data={$form} />
19+
20+
<h3>This is from +layout.svelte</h3>
21+
22+
{#if $message}
23+
<div
24+
class="status"
25+
class:error={$page.status >= 400}
26+
class:success={$page.status == 200}
27+
>
28+
{$message}
29+
</div>
30+
{/if}
31+
32+
<form method="POST" action="/tests/issue-230" use:enhance>
33+
<label>
34+
Name<br />
35+
<input
36+
name="name"
37+
data-invalid={$errors.name}
38+
bind:value={$form.name}
39+
/>
40+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
41+
</label>
42+
43+
<label>
44+
Email<br />
45+
<input
46+
name="email"
47+
type="email"
48+
data-invalid={$errors.email}
49+
bind:value={$form.email}
50+
/>
51+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
52+
</label>
53+
54+
<button>Submit</button>
55+
</form>
56+
57+
<hr />
58+
<p>
59+
<a target="_blank" href="https://superforms.vercel.app/api"
60+
>API Reference</a
61+
>
62+
</p>
63+
</div>
64+
65+
<div style="border: 2px solid green; padding: 10px; margin-top: 10px;">
66+
<slot />
67+
</div>
68+
69+
<style>
70+
.invalid {
71+
color: red;
72+
}
73+
74+
.status {
75+
color: white;
76+
padding: 4px;
77+
padding-left: 8px;
78+
border-radius: 2px;
79+
font-weight: 500;
80+
}
81+
82+
.status.success {
83+
background-color: seagreen;
84+
}
85+
86+
.status.error {
87+
background-color: #ff2a02;
88+
}
89+
90+
input {
91+
background-color: #ddd;
92+
}
93+
94+
a {
95+
text-decoration: underline;
96+
}
97+
98+
hr {
99+
margin-top: 4rem;
100+
}
101+
102+
form {
103+
padding-top: 1rem;
104+
padding-bottom: 1rem;
105+
}
106+
</style>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { superValidate, message } from '$lib/server';
2+
import { fail } from '@sveltejs/kit';
3+
import { z } from 'zod';
4+
5+
const schema = z.object({
6+
name: z.string().min(1),
7+
email: z.string().email()
8+
});
9+
10+
///// Form actions /////
11+
12+
export const actions = {
13+
default: async ({ request }) => {
14+
const form = await superValidate(request, schema);
15+
16+
console.log('POST', form);
17+
18+
if (!form.valid) return fail(400, { form });
19+
20+
return message(form, 'Form posted successfully!');
21+
}
22+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Actions } from './$types';
2+
3+
export const actions: Actions = {
4+
default: async ({ request }) => {
5+
// Data here
6+
const formData = await request.formData();
7+
console.log(formData);
8+
9+
return { success: true };
10+
}
11+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script lang="ts">
2+
import { enhance } from '$app/forms';
3+
import type { ActionData } from './$types';
4+
5+
export let form: ActionData;
6+
</script>
7+
8+
<div style="background-color: yellow;">
9+
This is +page.svelte, in the slot of Layout<br />Try Submitting the form
10+
below - which is a normal Svelte Form Action use:enhance
11+
</div>
12+
13+
<form style="margin-top: 10px;" method="POST" use:enhance>
14+
<input type="text" name="name" value="test" />
15+
<button type="submit">Submit</button>
16+
{#if form?.success}Posted!{/if}
17+
</form>

0 commit comments

Comments
 (0)