Replies: 1 comment 2 replies
-
I managed to get this to work for SPA (client-side) by looking at the superform docs. Example:
import { error } from '@sveltejs/kit';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { z } from 'zod';
export const _userSchema = z.object({
id: z.number().int().positive(),
name: z.string().min(2),
email: z.string().email()
});
export const load = async ({ fetch }) => {
const id = 5;
const request = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
if (request.status >= 400) throw error(request.status);
const userData = await request.json();
const form = await superValidate(userData, zod(_userSchema));
return { form };
};
<script lang="ts">
import { superForm, setMessage, setError } from 'sveltekit-superforms';
import { _userSchema } from './+page.js';
import { zod } from 'sveltekit-superforms/adapters';
import * as Form from '$lib/components/ui/form';
import Input from '$lib/components/ui/input/input.svelte';
export let data;
const form = superForm(data.form, {
SPA: true,
validators: zod(_userSchema),
onUpdate({ form }) {
// Form validation
if (form.data.email.includes('spam')) {
setError(form, 'email', 'Suspicious email address.');
} else if (form.valid) {
// TODO: Call an external API with form.data, await the result and update form
setMessage(form, 'Valid data!');
}
}
});
const { form: formData, enhance } = form;
</script>
<h1>Edit user</h1>
<!-- {#if $message}<h3>{$message}</h3>{/if} -->
<form method="POST" use:enhance>
<!-- <label>
Name<br />
<input
aria-invalid={$form.errors.name ? 'true' : undefined}
bind:value={$form.name}
{...$constraints.name}
/>
</label>
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
<label>
E-mail<br />
<input
type="email"
aria-invalid={$errors.email ? 'true' : undefined}
bind:value={$form.email}
{...$constraints.email}
/>
</label>
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if} -->
<Form.Field {form} name="name">
<Form.Control let:attrs>
<Form.Label>Name</Form.Label>
<Input {...attrs} bind:value={$formData.name} />
</Form.Control>
<Form.Description />
<Form.FieldErrors />
</Form.Field>
<Form.Field {form} name="email">
<Form.Control let:attrs>
<Form.Label>Email</Form.Label>
<Input {...attrs} bind:value={$formData.email} />
</Form.Control>
<Form.Description />
<Form.FieldErrors />
</Form.Field>
<button>Submit</button>
</form> |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am having trouble implementing client side only forms with zod validation just as is shown in the shadcn form component. Basically all I want is client side validation and be able to call a function after successful validation without using anything that starts with the word server 😉
Here is my react code:
And here is my svelte code:
But I get an error in my IDE with the following svelte code: superValidate returns a promise whose result should be passed to the Form component. Right now I am passing the promise to the
form
prop in the Form component.I am not very familiar with superforms and formsnap yet so forgive me if I am missing something obvious. All I want to accomplish is client side forms as shown in shadcn as mentioned above.
Thanks in advance for your time!
Beta Was this translation helpful? Give feedback.
All reactions